function Gadget(name, color)
{
this.name = name;
this.color = color;
}
Gadget.prototype.rating = 3
var newtoy = new Gadget("webcam", "black")
newtoy.constructor.prototype.constructor.prototype.constructor.prototype
It always returns the object with rating = 3.
But if I do the following:
newtoy.__proto__.__proto__.__proto__
The chain ends up returning null
.
Also in Internet Explorer how would I check the null if there is not a __proto__
property?
Object
is Eve, andFunction
is Adam, Adam (Function
) uses his bone (Function.prototype
) to create Eve (Object
). Then who created Adam (Function
)? -- The Inventor of the JavaScript language :-).According to utsaina's answer, I want to add more useful info.
It should NOT be.
Object.__proto__
should NOT point toObject.prototype
. Instead, the instance ofObject
o
,o.__proto__
should point toObject.prototype
.(Forgive me for using the terms
class
andinstance
in JavaScript, but you know it :-)I think the class
Object
itself is an instance ofFunction
, that's whyObject.__proto__ === Function.prototype
. Therefore:Object
is Eve, andFunction
is Adam, Adam (Function
) uses his bone (Function.prototype
) to create Eve (Object
).Furthermore, even the class
Function
itself is an instance ofFunction
itself, that isFunction.__proto__ === Function.prototype
, that's also whyFunction === Function.constructor
Further furthermore, the regular class
Cat
is an instance ofFunction
, that isCat.__proto__ === Function.prototype
.The reason for the above is, when we create a class in JavaScript, actually, we are just creating a function, which should be an instance of
Function
.Object
andFunction
are just special, but they are still classes, whileCat
is a regular class.As a matter of factor, in Google Chrome JavaScript engine, the following 4:
Function.prototype
Function.__proto__
Object.__proto__
Cat.__proto__
They are all
===
(absolutely equal) to the other 3, and their value isfunction Empty() {}
OK. Then who creates the special
function Empty() {}
(Function.prototype
)? Think about it :-)If all those figures were overwhelming, let's take a look what the properties mean.
STH.prototype
When creating a new function, there is an empty object being created in parallel and linked to the function with
[[Prototype]]
chain. To access this object, we useprototype
property of the function.Bear in mind that
prototype
property is only available for functions.STH.constructor
The prototype object mentioned above has no properties except for one -
constructor
. This property represents a function that created the prototype object.When creating
Gadget
function, we created an object like{constructor: Gadget}
as well - that is nothing likeGadget.prototype
. Asconstructor
refers to a function that created an object prototype,toy.constructor
representsGadget
function. We writetoy.constructor.prototype
and we are getting{constructor: Gadget}
again.Therefore, there's a vicious circle: you can use
toy.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype.constructor.prototype
and it always will beGadget.prototype
.STH.__proto__
While
prototype
is a property specific for functions,__proto__
is available for all objects as it lays inObject.prototype
. It refers to prototype of a function that can create an object.Here,
toy.__proto__
isGadget.prototype
. AsGadget.prototype
is an object ({}
) and objects are created withObject
function (see the example above), we getObject.prototype
. This is the higher object in JavaScript and its__proto__
can only indicatenull
.Every functions creates it's prototype. And when we create an object using that function constructor then the __proto__ property of my object will start pointing to the prototype of that function.
The Prototypal Inheritance in JavaScript is based on
__proto__
property in a sense that each object is inheriting the contents of the object referenced by its__proto__
property.The
prototype
property is special only forFunction
objects and only when usingnew
operator to call aFunction
as constructor. In this case, the created object's__proto__
will be set to constructor'sFunction.prototype
.This means that adding to
Function.prototype
will automatically reflect on all objects whose__proto__
is referencing theFunction.prototype
.Replacing constructor's
Function.prototype
with another object will not update__proto__
property for any of the already existing objects.Note that
__proto__
property should not be accessed directly, Object.getPrototypeOf(object) should be used instead.To answer the first question, I've created a bespoke diagram of
__proto__
andprototype
references, unfortunately stackoverflow does not allow me to add the image with "less than 10 reputation". Maybe some other time.[Edit] The figure uses
[[Prototype]]
instead of__proto__
because that is how ECMAScript specification refers to internal objects. I hope you can figure everything out.Here are some hints to help you understand the figure:
Note that
constructor
property does not exist in created objects, but is inherited from the prototype.constructor
is a pre-defined [[DontEnum]] property of the object pointed to by theprototype
property of a function object and will initially point to the function object itself.__proto__
is equivalent to the internal [[Prototype]] property of an object, ie its actual prototype.When you create an object with the
new
operator, its internal [[Prototype]] property will be set to the object pointed to by the constructor function'sprototype
property.This means that
.constructor
will evaluate to.__proto__.constructor
, ie the constructor function used to create the object, and as we have learned, theprotoype
property of this function was used to set the object's [[Prototype]].It follows that
.constructor.prototype.constructor
is identical to.constructor
(as long as these properties haven't been overwritten); see here for a more detailed explanation.If
__proto__
is available, you can walk the actual prototype chain of the object. There's no way to do this in plain ECMAScript3 because JavaScript wasn't designed for deep inheritance hierarchies.I really don't know why people didn't correct you about where the actual problem in your understanding.
This would make a lot easier for you to spot the problem
So let's see what's going on :
Great , so now let's look at this
__proto__
Before that , please remember 2 things regarding
__proto__
:When you create an object with the
new
operator, its internal[[Prototype]]
/proto__
property will be set to theprototype
property(1) of itsconstructor function
or "creator" if you like .Hard coded within JS — :
Object.prototype.__proto__
isnull
.Let's refer to these 2 points as "
bill
"Better?