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)!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
