Prototype prototype chain constructor inheritance

JS prototype and prototype chain
window have three properties globally, object array function, they all have many prototypes, and prototypes have many methods. These methods are for calling when the object array function is declared.

Then we declare an object var a = {}, we want to use the toString () method on the object, how to use it? Every object has the property __proto__, which points to an Object.prtotype. Find the toString () method on it.
As for the prototype chain, the object also has the attribute __proto__. When looking for a property or method, it will be found along the prototype chain until it is null

Constructor and inheritance

Here first create a constructor, then create a new object with new. A class of Student was created with the goods.
We did not reuse a function, so we need to reuse the Person class.

First, when calling this function directly, this refers to the window global object. Second, if you want to call through an object method, this refers to the object before the method.
Because this always points to the object that owns him.
At this time we need to use call apply to change the point of this to the current Student object

For example, when Person above is called directly, this refers to the global window. When using Person.apply (p2), this refers to p2

Use apply to make a change to this, passing in this and this [name, age] and this parameter. Implement a function that calls the parent class, and point this of the parent class to the child class.

apply and call are very similar, except that the parameters are in the form of an array and an object, so
that the inheritance of the constructor is realized.

The problem is that the method is usually placed on the prototype, and the attribute is placed on the object in the form of a combination to define or call the base class Person.
When inheriting, only the part of the object is inherited, and the part of the prototype is not inherited. Speaking of inheritance of the prototype chain.

First create a base class, place the line of the object on the object, and the method on the prototype.

Then we create a subclass that inherits the properties of the base class on the object. Then let the prototype of the subclass point to the prototype of the base class. Then add another method on the prototype of the subclass, like this.

The process of this sentence in the middle is that Sudent's __proto__ points to an empty object, and the empty object also has a proto pointing to Person's prototype, so it is searched on the prototype chain.

Published 11 original articles · praised 0 · visits 244

Guess you like

Origin blog.csdn.net/Douglas_Ryan_/article/details/105436551