In my JavaScript project I define an object, then create multiple instances using Object.create(). The object has several (string and int) properties, each of which is unique per instance. However, if I use an array property, all instances share the same array.
This code easily demonstrates this:
TestThing = {
code: "?",
intlist: [],
addint(i) {
alert("Adding " + i + " to " + this.code + ", list had " + this.intlist.length + " ints");
this.intlist.push(i);
}
}
var thing1 = Object.create(TestThing);
thing1.code = "Thing 1";
var thing2 = Object.create(TestThing);
thing2.code = "Thing 2";
thing1.addint(11);
thing2.addint(42);
alert(thing2.intlist); // will output 11,42
So, what causes this? How do I solve this issue?