This is the most basic piece of code to create an object using a constructor
function Person() {}
Person.prototype.name = 'feizai'
var person = new Person()
person.name // feizai
Copy the code
Core knowledge
Each function has a prototype that points to its prototype object 3. Each object has its own attribute __proto__ pointing to the protoobject 4 of its constructor. Each prototype starts with a constructor property pointing to its object's constructor, but a direct assignment of Person.prototype = {} would lose constructor. Bind 5 manually. If the access object attribute is not found in itself, it will go all the way to the prototype chain according to __proto__Copy the code
The Person function has a prototype object called Person.prototype. The Person object is derived from new Person(), so person.__proto__ === Person.prototype
Advanced a
As mentioned above, a function is both an object and a function. As a function it has a prototype property, and as an object it has a __proto__ property. Functions have both __proto__ and prototype
A printPerson.__proto__
What is this? __proto__ is the prototype of its constructor, and in the same way Person.__proto__ is the prototype of the constructor of Person, which is Function
Let’s look at Function first
Function is a Function, so it has __proto__ and prototype
Person.__proto__ === Function. Prototype = new Function()
Now let’s look at function.__proto__
This output looks exactly like function. prototype
__proto__ === function.prototype
Advanced 2
Person.__proto__ We know that Person.prototype is an object, so Person.prototype has a __proto__ attribute
Let’s look at person.prototype.__proto__
As stated above, __proto__ refers to the constructor’s prototype, which is our Obejct. Prototype
__proto__ will end up pointing to object. prototype and end up null
Advanced conclusion
Object. Prototype's __proto__ is null.Function's __proto__ is equivalent to prototypeCopy the code
Finally, attach a self-organized picture