Constructor creates the object

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

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

prototype

Each function has a Prototype property

Each jsvaScript object (except null), when created, is associated with another object. This object is called a stereotype, and each object “inherits” properties from the stereotype.

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

proto

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

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

constractor

Each stereotype has a constractor property that points to the associated constructor instance. The stereotype points to the constructor

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

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

console.log(person.__proto__ == Person.prototype) // true
console.log(Person.prototype.constructor == Person) // true
// By the way, learn 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 = 'Kevin';

var person = new Person();

person.name = 'Daisy';
console.log(person.name) // Daisy

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

In this example, we added the name attribute to the instance object Person, and when we print person.name, the result is naturally Daisy.

But when we remove the name property of person, we read person.name, we can’t find the name property from the Person object we get the name property from the person prototype which is person.proto, That’s looking in person. prototype, and luckily we found the name attribute, which turns out to be Kevin.

The prototype

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

! [] (

Prototype chain

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

By default, JavaScript does not copy the properties of an object. Instead, JavaScript simply creates an association between two objects, so that one object can access the properties and functions of the other object through a delegate, so it’s more accurate to call delegate rather than inheritance