Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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.

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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"};
    }

  • #3
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,294
    Thanks
    10
    Thanked 583 Times in 564 Posts
    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.
    Last edited by rnd me; 12-18-2008 at 01:44 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #5
    New to the CF scene
    Join Date
    Dec 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    rnd_me, i have to thank to you a lot.
    You've simplified my life so much, thanks!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •