For some reason, when I have an array in my class definition, and I then use the push() function to append to my array, the array variable seems to be updated for all future objects of that class that I create.
Here is the code I'm using:
var CircleSprite = cc.Class.extend({
circles:[],
thing: "",
ctor:function(target, n, x, y) {
this.num = n;
//This should always print 0 whenever a new object is created, but it's storing the object from the previous creation as well
cc.log("num circles: " + this.circles.length);
for(var i=0; i<n; i++)
{
var circleSprite = new cc.Sprite.create(res.circle);
circleSprite.setPosition(cc.p(x, y+circleSprite.height*this.circles.length));
this.circles.push(circleSprite); //doesn't work like I want
//this.circles = this.circles.concat(circleSprite); works well
}
//This code works as expected for each object
cc.log("this thing: " + this.thing); //prints ""
this.thing = "hi"+n;
cc.log("this thing: " + this.thing); //print "hi2", "hi3", etc.
},
However, when I use the concat() function instead to append, the array works as expected and is only updated for each object.
So why does push() modify the variable for all instances?