Deep understanding of javascript prototype and prototype chain

One, function object __proto: all reference types (functions, arrays, objects) have properties (implicit prototypes) Prototype: all functions have th...
1. Difference between prototype and proto
2. Who does the proto attribute point to
3. What is prototype chain
One, function object
  1. __proto: all reference types (functions, arrays, objects) have properties (implicit prototypes)
  2. Prototype: all functions have the prototype property (explicit prototype) (functions only)
  3. Constructor all prototype s have a constructor property that points to the associated constructor

When we declare a function keyword method, we will add a prototype property to the method, pointing to the default prototype object, and the constructor property of the prototype also points to the method object. These two properties are referenced by the object's properties when the object is created.

function Hello() {}; // Constructor var h = new Hello(); // Instanced object // Constructor has a prototype property console.log(Hello.prototype); // Object {} // The prototype property of a constructor has a constructor property that points to the constructor itself console.log(Hello.prototype.constructor === Hello); // true // Instantiated object has no prototype property console.log(h.prototype); // undefined // The constructor property of the instantiated object points to the constructor itself console.log(h.constructor); // function Hello(){} // Namely console.log(h.constructor === Hello.prototype.constructor); // true // getPrototypeOf is to get the__ console.log(Object.getPrototypeOf(h) === Hello.prototype); // true // Namely console.log(h.__proto__ === Hello.prototype); // true console.log(h.__proto__ === h.constructor.prototype); //true // Namely console.log(Hello.prototype === h.constructor.prototype); //true

1. Difference between prototype and proto

2. Who does the proto attribute point to

/*1,Literal measure*/ var a = {}; console.log(a.constructor); //Function Object() {[native code]} (constructor Object) console.log(a.__proto__ === a.constructor.prototype); //true /*2,Constructor mode*/ var A = function (){}; var a = new A(); console.log(a.constructor); // function() {} (that is, constructor function A) console.log(a.__proto__ === a.constructor.prototype); //true /*3,Object.create()mode*/ var a1 = {a:1} var a2 = Object.create(a1); console.log(a2.constructor); //Function object() {[native code]} (constructor Object) console.log(a2.__proto__ === a1);// true console.log(a2.__proto__ === a2.constructor.prototype); //false (this is the exception in Figure 1)

3. What is prototype chain

2 December 2019, 22:13 | Views: 6237

Add new comment

For adding a comment, please log in
or create account

0 comments