Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why functions prototype is chained repeatedly?

Tags:

javascript

I'm very new to JavaScript. I'm reading from JavaScript good parts. It says :

Every function object is also created with a prototype property

So I did something like this :

function test() {
}

console.log(test.prototype);

Using Chrome's developer tools, I find the output as follows :

enter image description here

I'm really confused with this output. Why does constructor's prototype property again nested with constructor? And why does this goes on like a chain? Where I'm missing the concept?

Thanks in advance.

like image 394
Ant's Avatar asked May 30 '12 07:05

Ant's


People also ask

What is prototype chaining?

The prototype of an object would also have a prototype object. This continues until we reach the top level when there is no prototype object. This is called prototype chaining or prototype chain in JavaScript. The properties defined on the prototype objects are also accessible to the object instance.

What is the difference between __ proto __ and prototype?

The prototype property is set to function when it is declared. All the functions have a prototype property. proto property that is set to an object when it is created using a new keyword. All objects behavior newly created have proto properties.

Are prototypes created for every function?

The prototype is an object that is associated with every functions and objects by default in JavaScript, where function's prototype property is accessible and modifiable and object's prototype property (aka attribute) is not visible. Every function includes prototype object by default.

Which object we get at the end of prototype chain?

That prototype object has a prototype of its own, and so on until an object is reached with null as its prototype. By definition, null has no prototype, and acts as the final link in this prototype chain.


2 Answers

The prototype property of a function holds the object from which all instances of that function will inherit when created with the new operator. And all these prototype objects (usually) have a constructor property which points back to the function - there you have the circular reference. So, as a new test() inherits that property, (new test).constructor === test evaluates to true.

You will need to distinguish between the prototype property of a function object and the prototype object from which an object inherits - often referenced as "the internal [[prototype]] property".

A constructor is a function, not to say a Function, and has both. Therefore it inherits from the Function.prototype object - where the constructor property says that all functions are constructed by the Function constructor. If your developers console would show the prototype of Function objects, you could see them. I think there is an option in the settings.

So, the famous "prototype chain" is not about the constructor and/or prototype properties, but about the prototype object from which that object inherits from:

 function test() {}              new test()
   (a Function)              (a test instance)
        ||                           ||
        ||                           ||
        \/                           \/
 Function.prototype            test.prototype
(a Function, by spec)           (an Object)
        ||                           ||
        ||                           ||
        \/                           \/
 Object.prototype             Object.prototype
        ||                           ||
        ||                           ||
        \/                           \/
       null                         null
like image 89
Bergi Avatar answered Oct 19 '22 22:10

Bergi


Thats one of the very good books to pick up.

It covers more of javascript from programmers point of view covering all the object oriented techniques and most of the things in it aren't covered in any other books on javascript.

And about prototype. Every object in JavaScript holds a hidden piece of state – a reference to another object known as the object’s prototype. Prototype objects in JavaScript give us inheritance, and they allow us to share method implementations, too. Prototypes also chain. In other words, since a prototype object is just an object, then a prototype object can maintain a reference to another prototype object.

Prototype in Javascript is little complicated part as I have experienced while learning it.

This is a great reference to know how prototype in js works.

like image 29
Katti Avatar answered Oct 19 '22 20:10

Katti