Prototype and prototype chain

Proto with the prototype

Pick up where we left off with the prototype from the last post

All functions have the Prototype attribute

Each object has an attribute __proto__ that points to the constructor’s prototype object, because the object’s __proto__ and its constructor’s prototype refer to the same memory address, so they are exactly equal

function Ninja(name, age){  
    this.name = name;
    this.age = age;
}

Ninja.prototype.run = function () {
    console.log('run')}let Naruto = new Ninja('Naruto'.18)
console.log(Naruto.__proto__ === Ninja.prototype)  // true
Copy the code

__proto__ is called an implicit prototype, and prototype is called an explicit prototype

We can access methods in the prototype constructor of an object with __proto__

Prototype chain

When accessing a method on an object, the constructor first looks for the method on the object itself. If it doesn’t find the method, the constructor’s __proto__(prototype) __proto__ is searched. And so you go up layer by layer and you create a chain, which we call a prototype chain.

The top layer of the prototype chain is Object.prototype.__proto__ (null)

Relationships between prototype chains, constructors, and instance objects:

thinking

1.Object,Array,Boolean, what is the __proto__ of these built-in function objects

Function. Prototype: These constructors are made of Function

console.log(Object.__proto__ === Function.prototype)    // true
console.log(Array.__proto__ === Function.prototype)     // true
console.log(String.__proto__ === Function.prototype)    // true
Copy the code

2.Function.__proto__ = Function. Prototype: the __proto__ of this object refers to the constructor of this object

console.log(Function.__proto__ === Function.prototype)  //true
Copy the code

The end of the

This is my study notes, there may be some shortcomings, if there is a mistake, welcome to correct, study progress together. And finally, the Nets champion!