prototype chain confusuion

X

xdevel1999

Hi,

if I've:

var B = {a: 11, b:12};


var F = function() { };

F.prototype = B;

var C = F;

here C.a is undefined but C.prototype.a is correct?

var C = new F();

here C.a is not undefined but C.protoype.a says that prototype is
undefined?

Any help is very appreciated!

Thanks
 
R

RobG

Hi,

if I've:

var B = {a: 11, b:12};

var F = function() { };

F.prototype = B;

var C  =  F;

here C.a is undefined  but C.prototype.a is correct?

Yes. The key here is that objects don't inherit from their own public
prototype property, they inherit from their constructor's prototype.
Property resolution follows an internal [[prototype]] chain, which is
influenced by setting the public prototype property of constructors.

You replaced the original F.prototype (which was initially a reference
to Function.prototype) with a reference to B. Therefore, in resolving
C.a, first C (which is F) is checked. The script engine will then
check C's internal [[prototype]] property, which references
Function.prototype and since there is no a property there either, it
will check its [[prototype]] which is a reference to Object.prototype,
and so on until it get's to null.

var C = new F();

here C.a is not undefined but C.protoype.a says that prototype is
undefined?

Yes, because since C is now an "instance" of F, its internal
[[prototype]] property has a reference to F.prototype, which you set
earlier as a reference to B. To keep going, B's [[prototype]]
references Object.prototype, etc. A consequence is that to add
properties to an object's prototype chain, you put them on its
constructor's prototype[1], not the object's own prototype.

I hope that makes sense. :)


1. More correctly, on a prototype that is on the internal
[[prototype]] chain. The object's constructor prototype may have been
replaced in the meantime, so you may not be able to modify it anymore
as you can't access object.[[prototype]] directly.
 
T

Thomas 'PointedEars' Lahn

xdevel1999 said:
var B = {a: 11, b:12};

var F = function() { };

F.prototype = B;

F.prototype = {a: 11, b: 12};

would also have worked.
var C = F;

here C.a is undefined but C.prototype.a is correct?

Yes. You have made `C' a reference to the same object as `F' refers to:

F ---> [object Function] <--- C
|
'-- prototype ---> [object Object]
|
:-- a === 11
'-- b === 12

("a ---> b": a refers to b; "a -- b": b is a property of a; a === b: b is
the value of a)
var C = new F();

here C.a is not undefined but C.protoype.a says that prototype is
undefined?

Yes, `C' inherits `a' from `F.prototype' through the prototype chain here.

F ---> [object Function]
|
'-- prototype ---> [object Object] <- - -[object Object] <--- C
|
:-- a === 11
'-- ...

("a - - -> b": a inherits from b)

`C.prototype' yields `undefined'; `C' is not a reference to a/the Function
object. `undefined' has no property, not even `a', so a TypeError exception
is th


C.constructor.prototype.a would have worked, had you set

F.prototype = {constructor: F, a: 11, b: 12};

before:

F ---> [object Function]
^ |
| '-- prototype ---> [object Object] <- - -[object Object] <--- C
| |
| :-- constructor ----.
| '-- a === 11 '
| |
'-----------------------------------------------'

Or C.constructor would have worked, had you set

C.constructor = F;

before:

F ---> [object Function] <--------------------- constructor --.
| |
'-- prototype ---> [object Object] <- - -[object Object] <--- C
|
:-- a === 11
'-- ...

And, in Netscape/Mozilla.org JavaScript,

C.__proto__.a

had worked because C.__proto__ refers to the Object object that has `a' and
`b' as its own properties:

F ---> [object Function] ,-------------- __proto__ --.
| v |
'-- prototype ---> [object Object] <- - -[object Object] <--- C
|
:-- a === 11
'-- ...

To avoid confusion and side-effects, use names starting with capital letter
only for constructors and constants.


HTH

PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
xdevel1999 said:
var B = {a: 11, b:12};

var F = function() { };

F.prototype = B;
[...]
var C = new F();

here C.a is not undefined but C.protoype.a says that prototype is
undefined?

[...]
C.constructor.prototype.a would have worked, had you set

F.prototype = {constructor: F, a: 11, b: 12};

before:

F ---> [object Function]
^ |
| '-- prototype ---> [object Object] <- - -[object Object] <--- C
| |
| :-- constructor ----.
| '-- a === 11 '
| |
'-----------------------------------------------'

To be precise:

F ---> [object Function]
^ |
/ '-- prototype ---> [object Object] <- - -[object Object] <--- C
: |
: :-- constructor ----.
: '-- a === 11 :
: :
'-------------------------------------------'

("a - - -> b": a inherits from b)


PointedEars
 
E

Erwin Moller

Thomas 'PointedEars' Lahn schreef:
Thomas said:
xdevel1999 said:
var B = {a: 11, b:12};

var F = function() { };

F.prototype = B;
[...]
var C = new F();

here C.a is not undefined but C.protoype.a says that prototype is
undefined?
[...]
C.constructor.prototype.a would have worked, had you set

F.prototype = {constructor: F, a: 11, b: 12};

before:

F ---> [object Function]
^ |
| '-- prototype ---> [object Object] <- - -[object Object] <--- C
| |
| :-- constructor ----.
| '-- a === 11 '
| |
'-----------------------------------------------'

To be precise:

F ---> [object Function]
^ |
/ '-- prototype ---> [object Object] <- - -[object Object] <--- C
: |
: :-- constructor ----.
: '-- a === 11 :
: :
'-------------------------------------------'

("a - - -> b": a inherits from b)


PointedEars

Hi Thomas,

Out of curiousity: Did you type those ASCII shemes yourself, or do you
use some convenient utility?
I really like them. :)
Very clear.

Regards,
Erwin Moller

--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
 
T

Thomas 'PointedEars' Lahn

Erwin said:
Thomas 'PointedEars' Lahn schreef:
Thomas said:
xdevel1999 wrote:
var B = {a: 11, b:12};

var F = function() { };

F.prototype = B;
[...]
var C = new F();

here C.a is not undefined but C.protoype.a says that prototype is
undefined?
[...]
C.constructor.prototype.a would have worked, had you set

F.prototype = {constructor: F, a: 11, b: 12};

before:

F ---> [object Function]
^ |
| '-- prototype ---> [object Object] <- - -[object Object] <---C
| |
| :-- constructor ----.
| '-- a === 11 '
| |
'-----------------------------------------------'

To be precise:

F ---> [object Function]
^ |
/ '-- prototype ---> [object Object] <- - -[object Object] <--- C
: |
: :-- constructor ----.
: '-- a === 11 :
: :
'-------------------------------------------'

("a - - -> b": a inherits from b)


PointedEars

[...]
Out of curiousity: Did you type those ASCII shemes yourself,

Yes, I like ASCII art.
or do you use some convenient utility?

No, that would save time, but it would also take away all the fun.
(Subscribe to de.alt.rec.ascii-art or alt.ascii-art! :))
I really like them. :)
Very clear.

Thanks, although the SW-NE arrow could have been better :)
_
/'

|°01|\|73|)34|2S
 
T

Thomas 'PointedEars' Lahn

Thomas said:
xdevel1999 said:
var C = new F();

here C.a is not undefined but C.protoype.a says that prototype is
undefined?

Yes, `C' inherits `a' from `F.prototype' through the prototype chain here.

F ---> [object Function]
|
'-- prototype ---> [object Object] <- - -[object Object] <--- C
|
:-- a === 11
'-- ...

("a - - -> b": a inherits from b)

`C.prototype' yields `undefined'; `C' is not a reference to a/the Function
object. `undefined' has no property, not even `a', so a TypeError
exception is th

.... is thrown.


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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top