For the new, JavaScript prototype is a very annoying thing, one prototype easy to confuse with __proto__, and the various points between them is really a bit complicated, in fact, there have been a lot of articles on the market to try to say clearly, there is a so-called very classic diagram, The above painted a variety of lines, one will connect this one will connect that, to tell the truth I see very dizzy, not to mention fully understand. So I want to try it myself and see if I can break out the important knowledge points in the prototype and make it clear in the simplest chart form.
We know that the prototype is an object that other objects can implement for inheritance of attributes. But the NI besides prototype, have a __proto__ is used for why? How does it make a difference if you look like that? They all point to who, so chaos how to remember AH? What the hell is a prototype chain? Believe that many beginners even have some experience of the veteran are not necessarily completely clear, the following with three simple diagram, with some sample code to understand.
I. The difference between prototype and __proto__
var a = {};
Console.log (A.prototype); Undefined
console.log (a.__proto__);//object {}
var b = function () {}
Console.log (b.prototype);//b {}
Console.log (b.__proto__);//function () {}
/*1, literal means * *
var a = {};
Console.log (a.__proto__); Object {}
console.log (a.__proto__ = = A.constructor.prototype);//true
/*2, constructor mode/
var a = function () {} ;
var a = new A ();
Console.log (a.__proto__); A {}
console.log (a.__proto__ = = A.constructor.prototype);//true
/*3, object.create () way * *
var a1 = {A : 1}
var a2 = object.create (A1);
Console.log (a2.__proto__); Object {a:1}
console.log (a.__proto__ = = A.constructor.prototype);//false (here is the exception in Figure 1)
var A = function () {};
var a = new A ();
Console.log (a.__proto__); A {} (that is, the prototype object of constructor function A)
console.log (a.__proto__.__proto__);//object {} (that is, the prototype object of constructor function object)
Console.log (a.__proto__.__proto__.__proto__); Null
The above is the entire content of this article, this article uses three diagrams to explain the JavaScript prototype object and the prototype chain, the hope is helpful to everybody learning JavaScript.