I have a prototype object in Javascript, when I initialise a new instance of the prototype and update properties in the prototype, it updates for all elements. I understand that
Initialize mutable members in the constructor, not in the prototype. If it's in the prototype, it will be shared between all instances:
let Test = function () {
this.array = [];
}
Test.prototype = {
add: function (value) {
this.array.push(value)
}
}
let test1 = new Test();
let test2 = new Test();
test1.add(1);
test1.add(2);
console.log(test1.array);
console.log(test2.array);