Javascript is often described as a prototype-based language, in which each object has a prototype-based object, and the object integrates methods and properties from its prototype as a template.

Stereotype chains: A stereotype object may also own a stereotype and integrate methods and properties from it, layer by layer, and so on. This relationship is often referred to as a stereotype chain.

Replication in javascript is achieved by establishing a link between an object instance and its constructor (which is the _proto_ property, derived from the constructor’s prototype property), and then finding those properties and methods in the constructor by going up the prototype chain.

_proto_

Is a property that is unique to the object and refers to the object’s prototype

prototype

Prototype is the property of the constructor. Function prototype represents the prototype of the object

2. When the constructor performs the New operation (that is, when the instance object is created), it points the instance object’s _proto_ to the constructor’s Prototype property.

function A() {}
let a = new A()
a._proto_ === A.prototype
Copy the code

3. Therefore, when defining attributes or methods on the function’s prototype, the instance object itself will look for the prototype if it cannot find the corresponding attributes or methods.

  1. Javascript prototype object

Javascript the process of new an object

function Mother (lastName) {
    this.lastName = lastName
}
var son = new Mother('Da');
Copy the code

Create a new object son;

[[prototype]] (_proto_ connection);

son._proto_ = Mother.prototype;

The new object is bound to the function call’s this:

Mother.call(son, ‘Da’);

4. Execute the code in the constructor:

son.lastName;

5. If the function returns no value, the new object is automatically returned.

return this;

Javascript prototype chain

Illustrate how the prototype chain works: