The re-learn object-oriented JavaScript programming (inheritance)

1. Inheritance

ES only supports implementation inheritance and implementation inheritance mainly depends on its prototype chain to achieve.

2. prototype chain

ES concept prototype described in the chain, as a main chain and a method prototype implementation inheritance. The basic idea is to use the prototype to make a reference type inherits from another type of reference properties and methods.

Recall relationship constructors, prototypes and examples

Each constructor has a prototype object, the prototype object contains a pointer pointing to the constructor, and examples all contain a pointer to the prototype object. So if we make a prototype object is equal to another instance of the type. At this time, then the prototype object will contain a pointer pointing to another prototype, correspondingly, another prototype also contains a pointer pointing to another constructor.

Further, if the prototype is another example of another type, such progressive layers, it constitutes an example and the prototype chain. This is the basic concept prototype chain.

function SuperType(){
    this.property = true
}

SuperType.prototype.getSuperValue = function(){
    retrun this.property
}

function SubType(){
    this.subproperty = false
}

// 继承了 SuperType
SubType.prototype = new SuperType()
SubType.prototype.getSubValue = function(){
    retrun this.subproperty;
}

var instance = new SubType()
instance.getSuperValue()    // true
The above code defines two types:

SuperType and SubType. Each attribute has a type and a method, respectively. The main difference is:

SubType inherited SuperType, and inheritance is by creating an instance SuperType, and assign that instance SubType.prototype implementation. Essence prototype object implementation is rewritten, replacing it with a new instance of the type.

In other words, all the methods and properties that are at the instance of the SuperType, now also present in SubType.prototype in. After SubType.prototype to add a method, thus inheriting the basic properties and methods of the SuperType has added a new method. This realization of the relationship between instances and constructors and prototypes.

By implementing the prototype chain, extended search mechanism prototype Essentially, when accessing an instance attribute to read mode, searches for the first attribute in the examples. If the property is not found, the search will continue instance of the prototype. In the case of inheritance through prototype chain, the search process is to continue up the prototype chain. Until the last step to find the method. In the case of properties and methods can not be found, the search process is always forward of a ring to the prototype chain will stop.

3. Default prototype

All reference types are inherited by default Object, and this is inherited through the prototype chain to achieve. The default function is the prototype of all instances of Object therefore it will include a default prototype internal pointer Object.prototype. That is why all custom types are inherited toString (), valueOf () method of the default

4. Examples of the relationship between the prototype and

You may be determined and the relationships between the prototype example of two ways. The first way is to use the instanceof operator, the result returns true as long as the operator to test prototype chain occurs through instance constructor. as follows:

instance instanceof Object  // true

The second method is isPrototypeOf () method, the same, as long as there had prototype prototype chain, can be said to be the prototype example of the prototype chain are derived, and therefore isPrototypeOf () method also returns true

Object.prototype.isPrototypeOf(instance)    // true

5. The method of carefully defined

Subtype sometimes need to override a superclass types, or need to add a super-type method does not exist, but in any case, after the code to add prototype method must be placed statement replaces the prototype

function  SuperType() {
    this.property = true
}

SuperType.prototype.getSuperValue = function(){
    retrun this.property
}

function SubType() {
    this.subproperty = false
}
----
// 继承了SuperType
SubType.prototype = new SuperType()

// 添加新方法
SubType.prototype.getSubValue = function(){
    retrun this.subproperty
}
//重写超类型中的方法
SubType.prototype.getSuperValue = function(){
    retrun false
}
-----
var instance = new SubType()
instance.getSuperValue()    // false
The above code is defined in two spaced portions of the method.

The first method getSubValue () is added to a SubType, the second method getSuperValue () method a prototype is already present in the chain, but this method will override the original method of shielding.

In other words, when an instance SubType call getSuperValue (), this method is redefined called, but by calling getSuperValue SuperType examples of (), that will continue to call the original method, all must be with the SuperType Alternatively, after the prototype example, the definition of these two methods.

Note : that when inherited through the prototype chain to achieve, you can not create a prototype method uses object literal, because it would override the prototype.

Question 6. prototype chain

Although the prototype chain is very powerful, you can use it to implement inheritance, but there are some problems.

1, comprising a prototype from a reference value type. Prior to include reference to the prototype said attribute type values ​​are shared by all instances. So this is why in the constructor, and not the cause of the attributes defined in the prototype object. When inheritance is implemented by the prototype, the prototype will actually become an example of another type. Thus, the original instance properties will naturally into the current prototype properties.

2, when creating instances of subtype, can not pass parameters to the constructor of the supertype. Under fact it can be said there is no way all object instances do not affect the case, to the super-type constructor pass parameters a.

7. borrow Constructor

The use of super-type constructor call within a subtype constructor. That constructor may be performed on the newly created object by apply (), and call () method.

7.1 pass parameters

With respect to the prototype chain, borrow constructor has a big advantage, which can pass arguments to a constructor supertype subtype constructor.

function s(name) {
    this.name = name
}

function b() {  
    // 继承 s,同时还传递参数
    s.call(this, 'nnn')
    // 实例属性
    this.age = 23
}

let i = new B()

i.name // nn
i.age  // 29

7.2 borrow constructor problem

If only borrow constructor, then it can not avoid the presence of the constructor mode problem, methods are defined in the constructor, so the function reuse can not be achieved. In the method defined supertype prototype, in terms of the sub-types it is not visible. The results of all types can only use the constructor mode. So borrow constructor mode rarely used alone.

8. The combination of inheritance

Also called pseudo-classical inherited, it refers to the combination of the prototype and borrow chain in the art to a constructor. To play one of the inheriting mode of the strengths of both. Principle is to use the prototype of the prototype implementation inheritance chain properties and methods, and to achieve an instance attribute inherited by borrowed constructor.

9. prototypal inheritance

Create a new object by means of existing objects, first create a temporary constructor, and then pass the object as a prototype of the constructor, and returns a new instance of the temporary type.

10. Parasitic inheritance

Create a encapsulating function only for the process of succession, the function in some way enhanced in the interior of the object and then return the object. Likewise we can not do multiplexing function reduces the efficiency

11. summary

ES support object-oriented programming, but does not use the class or interface. Objects can be created and enhanced during code execution, it has a dynamic rather than a rigidly defined entity. In the absence of class, the plant model may be employed, the constructor mode, prototype pattern to create the object.

11.1 Factory Pattern

Create an object with a simple function, add properties and methods for the object, and then return the object. This mode is replaced by the constructor mode

11.2 constructor mode

Create custom reference types, as can be built to create an object instance to use as the new operator. However, the constructor model also has the disadvantage that each of its members can not be reused, including functions. Since the function can not be limited to any object. So there is no reason to no longer function shared between multiple objects.

11.3 prototype model

Use the prototype property of the constructor to specify those properties and methods should be shared. When used in combination and a prototype model constructor mode, using the constructor instance property is defined, and defined using the prototype shared properties and methods.

JS mainly achieved through the prototype inheritance chain. Prototyping strand is a prototype implementation of the instance is assigned to another type constructor. In this way, the child will be able to access all types of properties and methods of the supertype. This and class-based inheritance is very similar.

Problems prototype chain is shared object instance inherits all the properties and methods, and therefore not suitable for use alone. If you want to solve this problem we need the aid of a constructor that calls the superclass constructor type within the sub-type constructor. This can be done in each instance has its own properties, while ensuring only the constructor mode defines the type.

11.4 prototypal inheritance

Inheritance can be implemented without predefined constructor, which essentially perform a shallow copy of a given object. And get a copy of the copy can be further reform

11.5 Parasitic inheritance

And prototypal inheritance is very similar, but also create an object based on an object or some information, and then enhance the object, and finally return the object. In order to solve the combination of inheritance patterns due to multiple calls over the type constructor which led to inefficiencies, this mode can be used with a combination of inheritance and

11.6 Combined parasitic inheritance

Parasitic collector inheritance and advantages of a combination of inheritance, the most effective way is based on the type inheritance

Public concern small number [students] yao

No small public tender classmates

Guess you like

Origin www.cnblogs.com/lieone/p/11611528.html