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