Go Back   CodingForums.com > :: Client side development > JavaScript programming > DOM and JSON scripting

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-17-2008, 06:51 PM   PM User | #1
CyanFloor
New to the CF scene

 
Join Date: Dec 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
CyanFloor is an unknown quantity at this point
Writing prototype properties cause the overwriting of all instance proprierties

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.
CyanFloor is offline   Reply With Quote
Old 12-18-2008, 01:10 AM   PM User | #2
jkd
Senior Coder

 
jkd's Avatar
 
Join Date: May 2002
Location: metro DC
Posts: 3,163
Thanks: 1
Thanked 18 Times in 18 Posts
jkd will become famous soon enough
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:
Code:
function Klass() {
    this.params = {a: "a", b: "b"};
}
__________________
jasonkarldavis.com
jkd is offline   Reply With Quote
Old 12-18-2008, 10:23 AM   PM User | #3
CyanFloor
New to the CF scene

 
Join Date: Dec 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
CyanFloor is an unknown quantity at this point
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?
CyanFloor is offline   Reply With Quote
Old 12-18-2008, 10:47 AM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by CyanFloor View Post
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.

Quote:
Originally Posted by CyanFloor View Post
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


Quote:
Originally Posted by CyanFloor View Post
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.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%

Last edited by rnd me; 12-18-2008 at 01:44 PM..
rnd me is offline   Reply With Quote
Old 12-18-2008, 11:14 AM   PM User | #5
CyanFloor
New to the CF scene

 
Join Date: Dec 2008
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
CyanFloor is an unknown quantity at this point
rnd_me, i have to thank to you a lot.
You've simplified my life so much, thanks!
CyanFloor is offline   Reply With Quote
Reply

Bookmarks

Tags
oop, prototype

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:11 PM.


Advertisement
Log in to turn off these ads.