P
Piotr K
I came across a strange problem or I just don't understand how
prototype object works.. but first things first - take a look at this
code
var A = function() {}
A.prototype.r = [10];
var a1 = new A, a2 = new A;
a2.r[0] = 20;
alert(a1.r[0]); // = 20 WTF ?!
Can someone explain me why JS doesn't create array copy for every new
instance but simply points to the same array? Of course I can create
that array in constructor class like this
var A = function() { this.r = [10]; }
This works fine however if I do it in that way, hasOwnProperty will
return TRUE for 'r' while doing it with prototype will return FALSE.
prototype object works.. but first things first - take a look at this
code
var A = function() {}
A.prototype.r = [10];
var a1 = new A, a2 = new A;
a2.r[0] = 20;
alert(a1.r[0]); // = 20 WTF ?!
Can someone explain me why JS doesn't create array copy for every new
instance but simply points to the same array? Of course I can create
that array in constructor class like this
var A = function() { this.r = [10]; }
This works fine however if I do it in that way, hasOwnProperty will
return TRUE for 'r' while doing it with prototype will return FALSE.