JavaScript是如何实现继承的?六种方式

大多OO语言都支持两种继承方式: 接口继承和实现继承 ,而ECMAScript中无法实现接口继承,ECMAScript只支持实现继承,而且其实现继承主要是依靠原型链来实现,下文给大家技术js实现继承的六种方式,需要的朋友参考下

前言:大多OO语言都支持两种继承方式: 接口继承和实现继承 ,而ECMAScript中无法实现接口继承,ECMAScript只支持实现继承,而且其实现继承主要是依靠 原型链 来实现。

1.原型链

基本思想:利用原型让一个引用类型继承另外一个引用类型的属性和方法。

构造函数,原型,实例之间的关系:每个构造函数都有一个原型对象,原型对象包含一个指向构造函数的指针,而实例都包含一个指向原型对象的内部指针。

原型链实现继承例子:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

functionSuperType() {

this.property =true;

}

SuperType.prototype.getSuperValue =function() {

returnthis.property;

}

functionsubType() {

this.property =false;

}

//继承了SuperType

SubType.prototype =newSuperType();

SubType.prototype.getSubValue =function(){

returnthis.property;

}

varinstance =newSubType();

console.log(instance.getSuperValue());//true

2.借用构造函数

基本思想:在子类型构造函数的内部调用超类构造函数,通过使用call()和apply()方法可以在新创建的对象上执行构造函数。

例子:

?

1

2

3

4

5

6

7

8

9

10

11

functionSuperType() {

this.colors = ["red","blue","green"];

}

functionSubType() {

SuperType.call(this);//继承了SuperType

}

varinstance1 =newSubType();

instance1.colors.push("black");

console.log(instance1.colors);//"red","blue","green","black"

varinstance2 =newSubType();

console.log(instance2.colors);//"red","blue","green"

3.组合继承

基本思想:将原型链和借用构造函数的技术组合在一块,从而发挥两者之长的一种继承模式。

例子:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

functionSuperType(name) {

this.name = name;

this.colors = ["red","blue","green"];

}

SuperType.prototype.sayName =function() {

console.log(this.name);

}

functionSubType(name, age) {

SuperType.call(this,name);//继承属性

this.age = age;

}

//继承方法

SubType.prototype =newSuperType();

Subtype.prototype.constructor = Subtype;

Subtype.prototype.sayAge =function() {

console.log(this.age);

}

varinstance1 =newSubType("EvanChen",18);

instance1.colors.push("black");

consol.log(instance1.colors);//"red","blue","green","black"

instance1.sayName();//"EvanChen"

instance1.sayAge();//18

varinstance2 =newSubType("EvanChen666",20);

console.log(instance2.colors);//"red","blue","green"

instance2.sayName();//"EvanChen666"

instance2.sayAge();//20

4.原型式继承

基本想法:借助原型可以基于已有的对象创建新对象,同时还不必须因此创建自定义的类型。

原型式继承的思想可用以下函数来说明:

?

1

2

3

4

5

functionobject(o) {

functionF(){}

F.prototype = o;

returnnewF();

}

例子:

?

1

2

3

4

5

6

7

8

9

10

11

varperson = {

name:"EvanChen",

friends:["Shelby","Court","Van"];

};

varanotherPerson = object(person);

anotherPerson.name ="Greg";

anotherPerson.friends.push("Rob");

varyetAnotherPerson = object(person);

yetAnotherPerson.name ="Linda";

yetAnotherPerson.friends.push("Barbie");

console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"

ECMAScript5通过新增Object.create()方法规范化了原型式继承,这个方法接收两个参数:一个用作新对象原型的对象和一个作为新对象定义额外属性的对象。

?

1

2

3

4

5

6

7

8

9

10

11

varperson = {

name:"EvanChen",

friends:["Shelby","Court","Van"];

};

varanotherPerson = Object.create(person);

anotherPerson.name ="Greg";

anotherPerson.friends.push("Rob");

varyetAnotherPerson = Object.create(person);

yetAnotherPerson.name ="Linda";

yetAnotherPerson.friends.push("Barbie");

console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"

5.寄生式继承

基本思想:创建一个仅用于封装继承过程的函数,该函数在内部以某种方式来增强对象,最后再像真正是它做了所有工作一样返回对象。

例子:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

functioncreateAnother(original) {

varclone = object(original);

clone.sayHi =function() {

alert("hi");

};

returnclone;

}

varperson = {

name:"EvanChen",

friends:["Shelby","Court","Van"];

};

varanotherPerson = createAnother(person);

anotherPerson.sayHi();///"hi"

6.寄生组合式继承

基本思想:通过借用函数来继承属性,通过原型链的混成形式来继承方法

其基本模型如下所示:

?

1

2

3

4

5

functioninheritProperty(subType, superType) {

varprototype = object(superType.prototype);//创建对象

prototype.constructor = subType;//增强对象

subType.prototype = prototype;//指定对象

}

例子:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

functionSuperType(name){

this.name = name;

this.colors = ["red","blue","green"];

}

SuperType.prototype.sayName =function(){

alert(this.name);

};

functionSubType(name,age){

SuperType.call(this,name);

this.age = age;

}

inheritProperty(SubType,SuperType);

SubType.prototype.sayAge =function() {

alert(this.age);

}

以上内容给大家介绍了javascript实现继承的六种方式,希望对大家有所帮助!