define

Each instance object has a private property (called __ proto __) that points to its constructor’s prototype. The prototype object also has a prototype object of its own (__ proto __), cascading up until an object’s prototype object is null. Null, by definition, has no prototype and serves as the last link in the prototype chain.

Know the prototype

The prototype attribute refers to a function that has a prototype attribute. The prototype attribute refers to a function that has a prototype attribute. The prototype attribute refers to a function that has a prototype attribute. Let’s look at a piece of code

    function init(){}
    init.prototype.name = 'huxiaolei'
    let init1 = new init()
    let init2 = new init()
    console.log(init1.name) // huxiaolei
    console.log(init2.name) // huxiaolei

Copy the code

As the code shows, the function’s prototype points to an object that is the prototype of the instances created by the function, namely init1 and init2.

Each instance object has a private property (called __ proto__) that points to the prototype of its constructor. In this definition there is a property called __ proto__, If the constructor’s prototype is the same as the instance object’s, you can print it in the code to see if it is the same.

    console.log(init.prototype === init1.__proto__) // true
    console.log(init.prototype === init2.__proto__) // true
Copy the code

Verification successful, same thing! The graph is arranged as follows

So so far we know what the prototype is, but what about the prototype chain?

Prototype chain

Literally, a prototype chain is a series of prototypes linked together to form a chain. Of course this is only literally meant, so the concept of prototype chain is really defined in the second sentence, “… The prototype object also has a prototype object of its own (__ proto __), cascading up until an object’s prototype object is null. By definition, NULL has no prototype and serves as the last link in the prototype chain.

By definition, we know that the prototype object has its own prototype object, so when we read the properties of the instance, if we can’t find them, we look for the properties in the prototype associated with the object, if we can’t find them, we look for the prototype, all the way to the top.

function init(){} init.prototype.name = 'xiaolei' let init1 = new init() init1.name = 'hu' console.log(init1.name) // hu  delete init1.name; console.log(init1.name) // xiaoleiCopy the code

As shown in the example above, we add a name attribute to init1 and print ‘hu’ naturally for the first time.

Init1: name = ‘xiaolei’; init1: name = ‘xiaolei’;

So what is the prototype of the prototype, what is the end of the prototype chain?

We all call a prototype a prototype object, so a prototype is naturally an object, and we can create it as an object.

let obj = new Object()
obj.name = 'xiaolei'
console.log(obj.name) // xiaolei 
Copy the code

The prototype Object is actually generated by the Object constructor. As mentioned earlier, the proto of the instance points to the constructor’s prototype

So the concept of prototype chain is clear, the text is expressed as follows, see the picture at the end.

The prototype __proto__ –> constructor’s prototype __proto__ –>… –> null

Wait, why did this place end up pointing to NULL, where did null come from? Object. Prototype.__ proto __ is what

So NULL means no object, no need to go up. Nature is the last link in the prototype chain.

Finally, the diagram of the prototype chain is attached. The blue line is the prototype chain.

Images of this article were provided by Hu Yu’s blog

Excellent literature Reference

  • Develop a visual large screen production platform from scratch
  • Build a lightweight CMS system — simpleCMS with natural SSR support from scratch
  • Hu Hu’s blog -JavaScript