I'm having some trouble understanding why modifying a property in instance a
modifies the same property on instance b
.
var A = function (){
};
A.prototype.data = {
value : 0
};
var a = new A();
var b = new A();
console.log(a.data.value, b.data.value); // 0, 0
a.data.value = 5;
console.log(a.data.value, b.data.value); // 5, 5
Shouldn't the prototype keyword make the data
variable an instance variable ?
This seems not to be the case in this example which executes as expected:
var B = function (){
this.data = {
value : 0
};
};
var i = new B();
var j = new B();
console.log(i.data.value, j.data.value); // 0, 0
i.data.value = 5;
console.log(i.data.value, j.data.value); // 5, 0
I'm confused as why the prototype method wont work. Maybe i'm lacking some conceptual knowledge.