Confused about prototype chain resolution

C

Crazy Cat

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?
 
L

Lasse Reichstein Nielsen

Crazy Cat said:
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
 
T

T.J. Crowder

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,
 
J

Joost Diepenmaat

Crazy Cat said:
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.
 
T

Thomas 'PointedEars' Lahn

Crazy said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top