Daily in-depth study record front end questions, strive to become a face bully as soon as possible

prototype

Every JS object (except null) is associated with another object when it is created. This object is called a Prototype. Each object “inherits” properties from the stereotype.

For example

var obj = {
    name: "zhangsan"
}

var newObj = Object.create(obj)

newObj.name // zhangsan
Copy the code

In the example above, the newObj object’s [Prototype] property refers to the obj object. That is, the newObj object’s [Prototype] property refers to the OBj object and inherits the obj property.

Prototype attributes are assigned to all objects when they are created. The Prototype chain is called the “Prototype chain”. In general, the top layer of the chain is “Object

Each function has a Prototype attribute.

function Person(){}

Person.prototype.name = "zhangsan"

var person = new Person()

person.name // zhangsan
Copy the code

proto

Every JS object (except null) has a property called proto, which points to the prototype of the modified object.

Objects can access the Prototype property internally by __proto__, meaning we can access the Prototype of the object by __proto__.

Ps: __proto__ is a nonstandard method, in practice we should use object.getProtoTypeof () to get the prototype of an Object

function Person(){}

var person = new Person();

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 constructor

function Person(){}
person.__proto__ == Person.prototype // true
Person === Person.prototype.constructor // true

Object.getPrototypeOf(person) === Person.prototype // true
Copy the code

Supplementary notes:

function Person() {

}
var person = new Person();
person.constructor === Person// true
Copy the code

The constructor property is not available when the person. Constructor property is fetched from the person’s prototype. This property happens to be in the prototype, so:

person.constructor === Person.prototype.constructor
Copy the code

Examples and prototypes

When an instance property is read, if it cannot be found, it looks for the property in the stereotype associated with the object. If not, it looks for the stereotype until it reaches the topmost level.

Prototype chain

A quick review of the relationship between constructors, stereotypes, and instances: Each constructor has a stereotype object, which contains a pointer to the constructor, and each instance contains an internal pointer to the stereotype object. So what if we made the prototype object equal to an instance of another type? Obviously, the stereotype object will contain a pointer to another stereotype, which in turn will contain a pointer to another constructor. If the other prototype is an instance of another type, the relationship still holds. In this way, a chain of examples and prototypes is formed. This is the basic concept of the so-called prototype chain.

Object.prototype.__proto__ === null // true
Copy the code