Let’s not talk about concepts. Let’s talk about examples
const a = new Array(a); a.__proto__ = ? a.constructor =?Copy the code
Array.prototype and Array
a.__proto__
和Array.prototype
It’s a thing,prototype
Is unique to constructors;Array
Is a function,The blue dotted line
isArray
The thing to do isPrototype chain
A part of;
The __proto__ constructor assigns its prototype to the instance’s __proto__.
If it’s more abstract, let’s see
function A (name) {
this.name = name;
};
var a = new A('smart a'); ??
console.log(a.__proto__ === A.prototype); ??
console.log(A.__proto__ === Function.prototype); ??
console.log(Function.__proto__ === Function.prototype); ??
console.log(Function.prototype.__proto__ === Object.prototype); ??
Copy the code
What is the output?
console.log(a.__proto__ === A.prototype); // true
console.log(A.__proto__ === Function.prototype); // true
Copy the code
The first two are easy to understand
Since instance (a) is constructed by function A, it is true that instance (a) is constructed by function A. By whom? Function is the second easy explanation
What about the third one? Self equal to self?
console.log(Function.__proto__ === Function.prototype); // true
Copy the code
Function is a special kind of Function, and like Array, it is generated by constructors, and constructors are functions;
So go on and on, where does it end?
console.log(Function.prototype.__proto__ === Object.prototype); ??
console.log(Object.prototype.__proto__ === null); ??
Copy the code
Function. Protytype. __proto__ is equivalent to Object. Prototype; Function.
console.log(Object.prototype.__proto__ === null); // true
Copy the code
So finally, object.prototype. __proto__, according to the rule, its constructor is prototype, but here is the end of the prototype link null, here is not why, this is the specification;
The process of always looking up is called the prototype chain
conclusion
A variable’s __proto__ always refers to the prototype of its constructor, and its endpoint is null; And this process has been looking for through prototype, that is, the prototype chain;