Home  >  Article  >  Web Front-end  >  JavaScript object-oriented programming (inheritance implementation)

JavaScript object-oriented programming (inheritance implementation)

黄舟
黄舟Original
2017-03-01 15:06:111507browse

Many OO languages ​​support two inheritance methods: interface inheritance and implementation inheritance. Interface inheritance only inherits method signatures, while implementation inheritance inherits the actual methods. As mentioned before, interface inheritance is not possible in ECMAScript since functions have no signatures. ECMAScript only supports implementation inheritance, and its implementation inheritance mainly relies on the prototype chain. Here, we mainly explain prototype chain inheritance, borrowed constructor, combined inheritance, prototype inheritance, parasitic inheritance, parasitic combination inheritance, etc.

1. Prototype chain

ECMAScript describes the concept of prototype chain and uses prototype chain as the main method to implement inheritance. The basic idea is to use prototypes to let one reference type inherit the properties and methods of another reference type. Let’s briefly review the relationship between constructors, prototypes, and instances: Each constructor has a prototype object, the prototype object contains a pointer to the constructor, and the instance contains an internal pointer to the prototype object. So, what happens if we make the prototype object equal to an instance of another type? Obviously, the prototype object at this time will contain a pointer to another prototype, and accordingly, the other prototype also contains a pointer to another constructor. If another prototype is an instance of another type, then the above relationship still holds, and so on, layer by layer, a chain of instances and prototypes is formed. This is the basic concept of the so-called prototype chain.

function Person(){
this.name=”defaultName”;
} 
Person.property.doAction=function(){
alert(“talk”);
}
 
function Student(){
this.age=5;
}
Student.property=new Person();
Student.property.doSome=function(){
alert(“ homework”);
}

Although the prototype chain is very powerful and can be used to implement inheritance, it also has some problems. The main problem arises from prototypes containing reference type values. You may still remember that we introduced earlier that prototype properties containing reference type values ​​will be shared by all instances; this is why properties should be defined in the constructor instead of in the prototype object. When inheritance is implemented through prototypes, the prototype actually becomes an instance of another type. Therefore, the original instance attributes have naturally become the current prototype attributes. The second problem with the prototype chain is that when creating an instance of a subtype, you cannot pass parameters to the constructor of the supertype. In fact, it should be said that there is no way to pass parameters to the constructor of a supertype without affecting all object instances. Because of this, and the problems just discussed with reference types in prototypes, prototype chains alone are rarely used in practice.

2. Borrowing constructor

In the process of solving the problems caused by containing reference type values ​​in prototypes, developers began to use a method called borrowing constructor (constructor stealing) technique (sometimes called fake objects or classical inheritance). The basic idea of ​​this technique is quite simple, which is to call the supertype constructor inside the subtype constructor. Don't forget that functions are nothing but objects that execute code in a specific environment, so by using the apply() and call() methods you can also execute constructors on (in the future) newly created objects.

function Person(name){
this.name=name;
} 
Person.property.doAction=function(){
alert(“talk”);
}
Person.property.showName=function(){
alert(this.name);
}
function Student(){
Person.call(this,name);
this.age=5;
}

If you just borrow the constructor, you will not be able to avoid the problems of the constructor pattern - Methods are all defined in the constructor, so function reuse is out of the question. Moreover, methods defined in the prototype of the super type are also invisible to the subtype. As a result, all types can only use the constructor pattern. Considering these problems, the technique of borrowing constructors is rarely used alone.

3. Combination inheritance
Combination inheritance (combination inheritance), sometimes also called pseudo-classical inheritance, refers to the combination of prototype chain and borrowed constructor technology, so as to bring out the best of both. An inheritance model. The idea behind it is to use the prototype chain to achieve inheritance of prototype properties and methods, and to achieve inheritance of instance properties by borrowing constructors. In this way, function reuse is achieved by defining methods on the prototype, and each instance is guaranteed to have its own attributes.

function Person(name){
this.name=name;
this.loves=[“sing”,”paly games”]
}
Person.property.showLoves=function (){
alert(this.lovers);
}
function Student(name,age){
Person.class(this,name);
This.age=age;
}
Student.property=new Person();
Student.property.constructor=Student;
Student.property.showName=function(){
alert(this.name);
}

Combined inheritance avoids the shortcomings of prototype chains and borrowed constructors, combines their advantages, and becomes the most commonly used inheritance pattern in JavaScript. Furthermore, instanceof and isPrototypeOf() can also be used to identify objects created based on combined inheritance.

4. Prototypal inheritance

function object(o){
function F(){}
F.prototype = o;
return new F();
}

When there is no need to go to great lengths to create a constructor, but you just want one object to remain similar to another object, prototype Formal inheritance is perfectly capable. But don't forget that properties containing reference type values ​​will always share the corresponding value, just like using the prototype pattern.

5. Parasitic combined inheritance

The so-called parasitic combined inheritance means inheriting properties by borrowing constructors and inheriting methods through the hybrid form of the prototype chain. The basic idea behind

is: there is no need to call the constructor of the supertype in order to specify the prototype of the subtype. All we need is nothing more than a copy of the supertype

prototype. Essentially, you use parasitic inheritance to inherit the prototype of the supertype, and then assign the result to the prototype of the subtype

. The basic pattern of parasitic combinatorial inheritance is as follows.

function inheritPrototype(subType, superType){
var prototype = object(superType.prototype); //创建对象
prototype.constructor = subType; //增强对象
subType.prototype = prototype; //指定对象
}

个参数:子类型构造函数和超类型构造函数。在函数内部,第一步是创建超类型原型的一个副本。第二步是为创建的副本添加constructor 属性,从而弥补因重写原型而失去的默认的constructor 属性。最后一步,将新创建的对象(即副本)赋值给子类型的原型。这样,我们就可以用调用inheritPrototype()函数的语句,去替换前面例子中为子类型原型赋值的语句了。

集寄生式继承和组合继承的优点与一身,是实现基于类型继承的最有效方式。

 以上就是JavaScript面向对象编程(继承实现方式)的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn