I'm not an expert in js. But recently I started writing with prototypes and faced with such issue.
I have class Test and object (attributes) like a property of this class. Then I create instances collection with different attributes. Unfortunately, I get collection with the same attributes, which are the same with last instance attributes. I suppose it's easier to understand when you see the code.
function Test(id) {
this.init(id);
}
Test.prototype = {
attributes : {
id : null,
},
init : function (id) {
this.attributes.id = id;
}
};
var a = [0, 1, 2, 3, 4];
var b = {};
for(var i in a) {
b[i] = new Test(a[i]);
}
console.log(b);
Could you explain me a cause and how can I avoid this trouble? Thanks a lot