Pre –

The constructor creates an object:

function Person() {}var person = new Person();
person.name = 'Lily';
console.log(person.name) // Lily
Copy the code

Person is a constructor, and we use new to create an instance object, Person.

prototype

Every function has a Prototype property. Every JavaScript object (except null) is associated with another object when it is created. This object is called a prototype, and every object “inherits” properties from the prototype.

function Person() {

}
Person.prototype.name = 'Kevin';
var person1 = new Person();
var person2 = new Person();
console.log(person1.name) // Kevin
console.log(person2.name) // Kevin
Copy the code

Prototype is a function property; each Object is an instance Object of the Object constructor function

const a = {}
a.__proto__.constructor === Object // true
a.constructor === Object // true
Copy the code

proto

Every JavaScript object (except null) has a property called proto, which points to the object’s prototype

function Person() {}var person = new Person();
console.log(person.__proto__ === Person.prototype); // true
Copy the code

constructor

Each stereotype has a constructor property pointing to the associated constructor, and the instance stereotype points to the stereotype of the constructor

function Person() {}console.log(Person === Person.prototype.constructor); // true
Copy the code
function Person() {}var person = new Person();

console.log(person.__proto__ == Person.prototype) // true
console.log(Person.prototype.constructor == Person) // true
// Here is an example of an ES5 method to get a prototype of an object
console.log(Object.getPrototypeOf(person) === Person.prototype) // true
Copy the code

Examples and prototypes

function Person() {

}

Person.prototype.name = 'Job';

var person = new Person();

person.name = 'Ken';
console.log(person.name) // ken

delete person.name;
console.log(person.name) // Job
Copy the code

In this example, we add the name attribute to the instance object Person. When we print Person. name, the result will be Ken.

But when we delete the name property of person, and we read Person.name, and we don’t find the name property in the Person object, we get the name property from the prototype of Person, person.proto, In Person. Prototype, luckily we found the name property and the result was Job.

Prototype and prototype

var obj = new Object(a); obj.name ='Sersi'
console.log(obj.name) // Sersi
Copy the code

Prototype chain

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

By default, JavaScript does not copy the properties of a prototype object. Instead, JavaScript simply creates an association between two objects (constructors and instance objects), so that one object can access the properties and functions of the other via a delegate (__proto__), so instead of calling it “inheritance”, “Commission” would be more accurate.

function Person() {}var p = new Person();

console.log(p.__proto__); / / {constructor: ƒ}
console.log(Person.prototype); / / {constructor: ƒ}
console.log(p.__proto__ === Person.prototype); //true

console.log(Person.prototype.__proto__ === Object.prototype) //true
console.log(Person.prototype.__proto__.constructor === Object) // true
Copy the code
function Person() {
}

Person.prototype.name = 'Job';

var person = new Person();

console.log(person.name) // Job
console.log(person) // Person {}
console.log(person.__proto__) // {name: "Job", constructor: ƒ}
Copy the code