Reputation: 751
if I set a prototype value and make two instances
then update the prototype value in one instance
now other instance prototype value does not update, why?
code is
var A = function() {
}
A.prototype.u = 2;
var a = new A();
var b = new A();
a.u = 4
alert(a.u) // 4
alert(b.u) // 2
it's so unreasonable, it's prototype value not this value. right?
Upvotes: 2
Views: 210
Reputation: 943142
You aren't "updating the prototype value". You are writing the new value to the local object and not to the prototype chain. The local property masks the one higher up the chain.
alert(a.u);
looks at a
, finds a u
and alerts it.
alert(b.u);
looks at b
, doesn't find a u
, looks up the prototype chain, finds a u
and alerts it.
Compare:
var A = function() {
}
A.prototype.u = 2;
var a = new A();
var b = new A();
a.u = 4;
A.prototype.u = 6;
alert(a.u);
alert(b.u);
Upvotes: 6