Js prototype and prototype chain
Basic constructor
function Person() {}var person = new Person();
person.name = 'Kevin';
console.log(person.name) // Kevin
Copy the code
prototype
Each function has a Prototype attribute
Every JavaScript object (except null) is associated with another object when it is created, and this object is called a prototype, from which each object “inherits” properties.
function Person() {}// prototype is a function property
Person.prototype.name = 'Kevin'; // Field on the prototype, shared by all instances
var person1 = new Person();
var person2 = new Person();
console.log(person1.name) // Kevin
console.log(person2.name) // Kevin
Copy the code
__proto__
Every JavaScript object (except null) has a property called \_\_proto\_\_. This property points to the object’s prototype
function Person() {}var person = new Person();
console.log(person.__proto__ === Person.prototype); // true
Copy the code
Object.create changes the __proto__ content of the Object, not necessarily pointing to the prototype.
constructor
Each stereotype has a constructor property pointing to the associated constructor, and the instance stereotype points to the constructor
function Person() {}console.log(Person === Person.prototype.constructor); // true
Copy the code
Prototype and (basic) Prototype
var obj = new Object(a); obj.name ='Kevin'
console.log(obj.name) // Kevin
Copy the code
Prototype chain
JavaScript does not copy the properties of an object by default. Instead, JavaScript simply creates an association between two objects so that one object can access the properties and functions of the other through delegation, so rather than inheritance, delegation is more accurate
Reference: author: dramatic storage link: www.jianshu.com/p/be7c95714… Source: Jane Book