elvishimpersonators
elvishimpersonators

Reputation: 3

JavaScript, why am I getting the error "array is not defined in the function add"

Here is my code

var chars = new function() {
  this.array = new Array(0);
  this.add = function(x, y, speed, size, color, ai) {
    this.array[array.length] = {x: x, y: y, size: size, color: color, ai: ai};
  }
}

On line 35, the only line in the function add I am getting the error "array is not defined" why is this happening and yes I did try,

chars.array[array.length] = {x: x, y: y, size: size, color: color, ai: ai};

Upvotes: 0

Views: 50

Answers (2)

Satpal
Satpal

Reputation: 133403

I would suggest you to use push method. The push method appends values to an array.

 this.array.push({x: x, y: y, size: size, color: color, ai: ai});

Upvotes: 1

Mehran Hatami
Mehran Hatami

Reputation: 12961

you have to fix it to:

var chars = new function() {
    this.array = new Array(0);
    this.add = function(x, y, speed, size, color, ai) {
        this.array[this.array.length] = {x: x, y: y, size: size, color: color, ai: ai};
    }
}

I have changed this.array[array.length] to this.array[this.array.length]

Upvotes: 2

Related Questions