...

View Full Version : Writing prototype properties cause the overwriting of all instance proprierties



CyanFloor
12-17-2008, 07:51 PM
Hi all, this is my first post here but i like so much these forums.
I have a problem with the prototype object. If a prototype object contains a property that is an object itself, when i set a value of its properties throught the instance, it value will be written directly in the Class.prototype object, causing all instance have this new value...

Code:
---------------------------------------------------------
function Klass() {};

Klass.prototype = {

a: 10,

params: {
a: 'a',
b: 'b'
},

getVal: function() {
alert('getVal called!');
}

};


var klass = new Klass();
var klass2 = new Klass();

klass.params.a = 'new-a';

// this alerts 'new-a' instead of 'a' !!!!!!!!
alert(klass2.params.a);

// 'new-a'
alert(Klass.prototype.params.a);


Please help me, i've been trying for a week with no success.

Thanks in advice.

jkd
12-18-2008, 02:10 AM
klass.params gets the params object from the prototype, so when you set the "a" property, you set it on the prototype (and not the instance), thus it changing everywhere. What you need to do is set the object in the constructor function to give each instance its own unique copy:


function Klass() {
this.params = {a: "a", b: "b"};
}

CyanFloor
12-18-2008, 11:23 AM
Thanks a lot JKD.

Therefore is it true to say that putting properties in the prototype object that are object itselves is wrong?

Why it doesn't happen if i set scalar properties? For example:
--------------------------------------------
function Klass() {};

Klass.prototype = {

a: 10,

params: {
a: 'a',
b: 'b'
},

getVal: function() {
alert('getVal called!');
}

};


var klass = new Klass();
var klass2 = new Klass();

klass.a = 'new-a';

// is 'a'
alert(klass2.a);

-------------------------------------------


There ins't a way to "unlink" the object properties from the prototype?

rnd me
12-18-2008, 11:47 AM
Therefore is it true to say that putting properties in the prototype object that are object itselves is wrong?


i don't think it is wrong per sŤ, but most of the time, we manage the state of an object using it's properties. Generally, you will see .prototype used more for holding methods to act upon the objects. At times .prototype is used for inheritance.



Why it doesn't happen if i set scalar properties?


primitives like numbers, booleans, and strings are passed by value, whereas objects are passed by reference.
wikipedia explains the details well, but just know that when setting two variables to the same object, both instances share that one object.
setting two vars to a string will make each var its own copy.


objects (including arrays) = shared instances
primitives = isolated instances




There ins't a way to "unlink" the object properties from the prototype?


store them in the object themselves, not in the prototoype. jkd showed an example of how to do this.

remember there are two copies of Klass-built objects: klass1+klass2, but there is only one Klass.prototype (an object) that all instances of objects made from the Klass constructor will share. if you ask for klass2.a, and a is not a member of klass2, Klass.prototype will be looked at to see it it has an "a" property. if it does, Klass.prototype.a is returned, if not, undefined is returned.

CyanFloor
12-18-2008, 12:14 PM
rnd_me, i have to thank to you a lot.
You've simplified my life so much, thanks! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum