Confused about prototype chain resolution

Discussion in 'Javascript' started by Crazy Cat, Mar 12, 2008.

  1. Crazy Cat

    Crazy Cat Guest

    Hi, I'm trying to understand prototype chains. The following code in
    IE 6 does not give expected results -

    var myobject;

    function BaseObject()
    {


    }

    BaseObject.prototype.a = 3;


    var myobject = new Object();

    myobject.prototype = new BaseObject();

    alert(myobject.a);

    I would expect to see that myobject.a = 3 however the code indicates
    that it is undefined. However myobject.prototype.a is equal to 3. I
    thought that the compiler was supposed to search the prototype chain
    until it found a definition for a if it's not found in the object.

    What am I missing?
    Crazy Cat, Mar 12, 2008
    #1
    1. Advertising

  2. Crazy Cat <> writes:

    > Hi, I'm trying to understand prototype chains. The following code in
    > IE 6 does not give expected results -
    >
    > var myobject;
    >
    > function BaseObject()
    > {
    >
    >
    > }
    >
    > BaseObject.prototype.a = 3;
    >
    >
    > var myobject = new Object();
    >
    > myobject.prototype = new BaseObject();
    >
    > alert(myobject.a);
    >
    > I would expect to see that myobject.a = 3 however the code indicates
    > that it is undefined. However myobject.prototype.a is equal to 3. I
    > thought that the compiler was supposed to search the prototype chain
    > until it found a definition for a if it's not found in the object.
    >
    > What am I missing?


    You are missing that the prototype chain is hidden in the internals
    of the JavaScript interpreter. You are simply assigning a new property
    of "myobject", coincidentally called "prototype", but that has nothing
    to do with "myobject"'s prototype chain, which still points to
    Object.prototype.

    The prototype chain cannot be changed after the object has been
    created (unless some Javascript implementation adds extra features
    to the language, ofcourse).

    What you have is the prototype chains (represented by -proto->):
    myobject -proto-> Object.prototype
    and
    myobject.prototype -proto-> BaseObject.prototype -proto-> Object.prototype

    Notice that BaseObject and Object are functions. That is why their
    "prototype" property is interesting. When you use the new-operator
    on a function, you create a new object with the functions "prototype"
    property value (if it's an object) as prototype parent.

    Your "myobject" is not a function, it's just a plain object.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Mar 12, 2008
    #2
    1. Advertising

  3. Crazy Cat

    T.J. Crowder Guest

    On Mar 12, 3:32 pm, Crazy Cat <> wrote:
    > Hi, I'm trying to understand prototype chains. The following code in
    > IE 6 does not give expected results -
    >
    > var myobject;
    >
    > function BaseObject()
    > {
    >
    > }
    >
    > BaseObject.prototype.a = 3;
    >
    > var myobject = new Object();
    >
    > myobject.prototype = new BaseObject();
    >
    > alert(myobject.a);
    >
    > I would expect to see that myobject.a = 3 however the code indicates
    > that it is undefined. However myobject.prototype.a is equal to 3. I
    > thought that the compiler was supposed to search the prototype chain
    > until it found a definition for a if it's not found in the object.
    >
    > What am I missing?


    Expanding on what Lasse said, you seem to be trying to change the
    prototype of a specific object, rather than of a constructor function
    for objects; not only is that probably the problem, but also it's more
    useful to modify the constructor function so you can use it time and
    again. Below is an example of a simple three-level hierarchy. If you
    haven't already, it's worth reading Crockford (http://
    javascript.crockford.com, particularly in this regard
    http://javascript.crockford.com/prototypal.html and
    http://javascript.crockford.com/inheritance.html, although the latter
    is about simulating other language behaviors in JavaScript which
    somewhat limits the power of the language) and also David Flanagan's
    book ("JavaScript, the Definitive Guide", currently fifth edition,
    O'Reilly). The example below is very basic and not a substitute for
    reading up. :)

    Example:
    * * * *
    // Deepest base, has "a"
    function Bottom()
    {
    }
    Bottom.prototype.a = 1;

    // Mid-level, sets Bottom as prototype, adds "b"
    function Middle()
    {
    }
    Middle.prototype = new Bottom();
    Middle.prototype.b = 2;

    // Top-level, sets Middle as prototype, adds "c"
    function Top()
    {
    }
    Top.prototype = new Middle();
    Top.prototype.c = 3;

    function testIt()
    {
    var t;
    var m;
    var b;

    t = new Top();
    alert("t.a = " + t.a); // 1
    alert("t.b = " + t.b); // 2
    alert("t.c = " + t.c); // 3

    m = new Middle();
    alert("m.a = " + m.a); // 1
    alert("m.b = " + m.b); // 2
    alert("m.c = " + m.c); // undefined

    b = new Bottom();
    alert("b.a = " + b.a); // 1
    alert("b.b = " + b.b); // undefined
    alert("b.c = " + b.c); // undefined
    }
    * * * *

    Hope this helps,
    --
    T.J. Crowder
    tj / crowder software / com
    T.J. Crowder, Mar 12, 2008
    #3
  4. Crazy Cat <> writes:

    > Hi, I'm trying to understand prototype chains. The following code in
    > IE 6 does not give expected results -


    it won't work in any other conforming javascript/ecmascript engine
    either.

    > myobject.prototype = new BaseObject();


    you can't assign to the internal [[prototype]] property of an object
    directly. you can only assign to the public "prototype" property of a
    constructor and then create an object via that constructor.

    I recently wrote a short essay that should explain the distinction:

    http://joost.zeekat.nl/constructors-considered-mildly-confusing.html

    ps: At the moment the DNS server for my domain seems to be down, I'm
    working at it as we speak, so if that URL doesn't work, you could try
    again in an hour or so.

    HTH,

    Joost.

    --
    Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
    Joost Diepenmaat, Mar 12, 2008
    #4
  5. Crazy Cat wrote:
    > Hi, I'm trying to understand prototype chains. The following code in
    > IE 6 does not give expected results -
    >
    > var myobject;
    >
    > function BaseObject()
    > {
    >
    >
    > }
    >
    > BaseObject.prototype.a = 3;
    >
    >
    > var myobject = new Object();
    >
    > myobject.prototype = new BaseObject();
    >
    > alert(myobject.a);
    >
    > I would expect to see that myobject.a = 3 however the code indicates
    > that it is undefined. However myobject.prototype.a is equal to 3. I
    > thought that the compiler was supposed to search the prototype chain
    > until it found a definition for a if it's not found in the object.
    >
    > What am I missing?


    1. It would be not the compiler, but the byte-code interpreter of a Virtual
    Machine that follows the prototype chain. Byte-code is the likely result
    of what the script engine's compiler does.

    2. Object objects don't have a built-in prototype property, you are creating
    one here. Therefore the next object in myobject's prototype chain
    remains Object.prototype.

    3. Even if `myobject' was a reference to a constructor, the constructed
    prototype chain would not be what you would expect as you would have a
    BaseObject object before the BaseObject prototype object in the
    prototype chain. See previous discussions.


    PointedEars
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
    Thomas 'PointedEars' Lahn, Mar 13, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew FPGA
    Replies:
    0
    Views:
    953
    Andrew FPGA
    Sep 26, 2005
  2. June Lee
    Replies:
    2
    Views:
    785
    Jim Cobban
    Apr 13, 2008
  3. Alexis Nikichine

    Modifying prototype chain

    Alexis Nikichine, Mar 21, 2005, in forum: Javascript
    Replies:
    2
    Views:
    68
    Alexis Nikichine
    Mar 21, 2005
  4. xdevel1999

    prototype chain confusuion

    xdevel1999, Jan 23, 2009, in forum: Javascript
    Replies:
    6
    Views:
    81
    Thomas 'PointedEars' Lahn
    Jan 23, 2009
  5. Kees

    extend prototype chain?

    Kees, Jan 25, 2010, in forum: Javascript
    Replies:
    26
    Views:
    229
    Jorge
    Jan 29, 2010
Loading...

Share This Page