当前位置: 移动技术网 > IT编程>开发语言>JavaScript > JavaScript的六种继承方式(推荐)

JavaScript的六种继承方式(推荐)

2017年12月12日  | 移动技术网IT编程  | 我要评论

继承是面向对象编程中又一非常重要的概念,javascript支持实现继承,不支持接口继承,实现继承主要依靠原型链来实现的。

原型链

首先得要明白什么是原型链,在一篇文章看懂proto和prototype的关系及区别中讲得非常详细

原型链继承基本思想就是让一个原型对象指向另一个类型的实例

function supertype() { 
 this.property = true 
} 
supertype.prototype.getsupervalue = function () { 
 return this.property 
} 
function subtype() { 
 this.subproperty = false 
} 
subtype.prototype = new supertype() 
subtype.prototype.getsubvalue = function () { 
 return this.subproperty 
} 
var instance = new subtype() 
console.log(instance.getsupervalue()) // true 

代码定义了两个类型supertype和subtype,每个类型分别有一个属性和一个方法,subtype继承了supertype,而继承是通过创建supertype的实例,并将该实例赋给subtype.prototype实现的。

实现的本质是重写原型对象,代之以一个新类型的实例,那么存在supertype的实例中的所有属性和方法,现在也存在于subtype.prototype中了。

我们知道,在创建一个实例的时候,实例对象中会有一个内部指针指向创建它的原型,进行关联起来,在这里代码subtype.prototype = new supertype(),也会在subtype.prototype创建一个内部指针,将subtype.prototype与supertype关联起来。

所以instance指向subtype的原型,subtype的原型又指向supertype的原型,继而在instance在调用getsupervalue()方法的时候,会顺着这条链一直往上找。

添加方法

在给subtype原型添加方法的时候,如果,父类上也有同样的名字,subtype将会覆盖这个方法,达到重新的目的。 但是这个方法依然存在于父类中。

记住不能以字面量的形式添加,因为,上面说过通过实例继承本质上就是重写,再使用字面量形式,又是一次重写了,但这次重写没有跟父类有任何关联,所以就会导致原型链截断。

function supertype() { 
 this.property = true 
} 
supertype.prototype.getsupervalue = function () { 
 return this.property 
} 
function subtype() { 
 this.subproperty = false 
} 
subtype.prototype = new supertype() 
subtype.prototype = { 
 getsubvalue:function () { 
 return this.subproperty 
 } 
} 
var instance = new subtype() 
console.log(instance.getsupervalue()) // error 

问题

单纯的使用原型链继承,主要问题来自包含引用类型值的原型。

function supertype() { 
 this.colors = ['red', 'blue', 'green'] 
} 
function subtype() { 
} 
subtype.prototype = new supertype() 
var instance1 = new subtype() 
var instance2 = new subtype() 
instance1.colors.push('black') 
console.log(instance1.colors) // ["red", "blue", "green", "black"] 
console.log(instance2.colors) // ["red", "blue", "green", "black"] 

在supertype构造函数定义了一个colors属性,当subtype通过原型链继承后,这个属性就会出现subtype.prototype中,就跟专门创建了subtype.prototype.colors一样,所以会导致subtype的所有实例都会共享这个属性,所以instance1修改colors这个引用类型值,也会反映到instance2中。

借用构造函数

此方法为了解决原型中包含引用类型值所带来的问题。

这种方法的思想就是在子类构造函数的内部调用父类构造函数,可以借助apply()和call()方法来改变对象的执行上下文

function supertype() { 
 this.colors = ['red', 'blue', 'green'] 
} 
function subtype() { 
 // 继承supertype 
 supertype.call(this) 
} 
var instance1 = new subtype() 
var instance2 = new subtype() 
instance1.colors.push('black') 
console.log(instance1.colors) // ["red", "blue", "green", "black"] 
console.log(instance2.colors) // ["red", "blue", "green"] 

在新建subtype实例是调用了supertype构造函数,这样以来,就会在新subtype对象上执行supertype函数中定义的所有对象初始化代码。

结果,subtype的每个实例就会具有自己的colors属性的副本了。

传递参数

借助构造函数还有一个优势就是可以传递参数

function supertype(name) { 
 this.name = name 
} 
function subtype() { 
 // 继承supertype 
 supertype.call(this, 'jiang') 
 this.job = 'student' 
} 
var instance = new subtype() 
console.log(instance.name) // jiang 
console.log(instance.job) // student 

问题

如果仅仅借助构造函数,方法都在构造函数中定义,因此函数无法达到复用

组合继承(原型链+构造函数)

组合继承是将原型链继承和构造函数结合起来,从而发挥二者之长的一种模式。

思路就是使用原型链实现对原型属性和方法的继承,而通过借用构造函数来实现对实例属性的继承。

这样,既通过在原型上定义方法实现了函数复用,又能够保证每个实例都有它自己的属性。

function supertype(name) { 
 this.name = name 
 this.colors = ['red', 'blue', 'green'] 
} 
supertype.prototype.sayname = function () { 
 console.log(this.name) 
} 
function subtype(name, job) { 
 // 继承属性 
 supertype.call(this, name) 
 this.job = job 
} 
// 继承方法 
subtype.prototype = new supertype() 
subtype.prototype.constructor = supertype 
subtype.prototype.sayjob = function() { 
 console.log(this.job) 
} 
var instance1 = new subtype('jiang', 'student') 
instance1.colors.push('black') 
console.log(instance1.colors) //["red", "blue", "green", "black"] 
instance1.sayname() // 'jiang' 
instance1.sayjob() // 'student' 
var instance2 = new subtype('j', 'doctor') 
console.log(instance2.colors) // //["red", "blue", "green"] 
instance2.sayname() // 'j' 
instance2.sayjob() // 'doctor' 

这种模式避免了原型链和构造函数继承的缺陷,融合了他们的优点,是最常用的一种继承模式。

原型式继承

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

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

在object函数内部,先创建一个临时性的构造函数,然后将传入的对象作为这个构造函数的原型,最后返回这个临时类型的一个新实例。

本质上来说,object对传入其中的对象执行了一次浅复制。

var person = { 
 name: 'jiang', 
 friends: ['shelby', 'court'] 
} 
var anotherperson = object(person) 
console.log(anotherperson.friends) // ['shelby', 'court'] 

这种模式要去你必须有一个对象作为另一个对象的基础。

在这个例子中,person作为另一个对象的基础,把person传入object中,该函数就会返回一个新的对象。

这个新对象将person作为原型,所以它的原型中就包含一个基本类型和一个引用类型。

所以意味着如果还有另外一个对象关联了person,anotherperson修改数组friends的时候,也会体现在这个对象中。

object.create()方法

es5通过object.create()方法规范了原型式继承,可以接受两个参数,一个是用作新对象原型的对象和一个可选的为新对象定义额外属性的对象,行为相同,基本用法和上面的object一样,除了object不能接受第二个参数以外。

var person = { 
 name: 'jiang', 
 friends: ['shelby', 'court'] 
} 
var anotherperson = object.create(person) 
console.log(anotherperson.friends) // ['shelby', 'court'] 

寄生式继承

寄生式继承的思路与寄生构造函数和工厂模式类似,即创建一个仅用于封装继承过程的函数。

function createanother(o) { 
 var clone = object.create(o) // 创建一个新对象 
 clone.sayhi = function() { // 添加方法 
 console.log('hi') 
 } 
 return clone // 返回这个对象 
} 
var person = { 
 name: 'jiang' 
} 
var anotherpeson = createanother(person) 
anotherpeson.sayhi() 

基于person返回了一个新对象anotherpeson,新对象不仅拥有了person的属性和方法,还有自己的sayhi方法。

在主要考虑对象而不是自定义类型和构造函数的情况下,这是一个有用的模式。

寄生组合式继承

在前面说的组合模式(原型链+构造函数)中,继承的时候需要调用两次父类构造函数。

父类

function supertype(name) { 
 this.name = name 
 this.colors = ['red', 'blue', 'green'] 
} 

第一次在子类构造函数中

function subtype(name, job) { 
 // 继承属性 
 supertype.call(this, name) 
 this.job = job 
} 

第二次将子类的原型指向父类的实例

// 继承方法 
subtype.prototype = new supertype() 

当使用var instance = new subtype()的时候,会产生两组name和color属性,一组在subtype实例上,一组在subtype原型上,只不过实例上的屏蔽了原型上的。

使用寄生式组合模式,可以规避这个问题。

这种模式通过借用构造函数来继承属性,通过原型链的混成形式来继承方法。

基本思路:不必为了指定子类型的原型而调用父类的构造函数,我们需要的无非就是父类原型的一个副本。

本质上就是使用寄生式继承来继承父类的原型,在将结果指定给子类型的原型。

function inheritprototype(subtype, supertype) { 
 var prototype = object.create(supertype.prototype) 
 prototype.constructor = subtype 
 subtype.prototype = prototype 
} 

该函数实现了寄生组合继承的最简单形式。

这个函数接受两个参数,一个子类,一个父类。

第一步创建父类原型的副本,第二步将创建的副本添加constructor属性,第三部将子类的原型指向这个副本。

function supertype(name) { 
 this.name = name 
 this.colors = ['red', 'blue', 'green'] 
} 
supertype.prototype.sayname = function () { 
 console.log(this.name) 
} 
function subtype(name, job) { 
 // 继承属性 
 supertype.call(this, name) 
 this.job = job 
} 
// 继承 
inheritprototype(subtype, supertype) 
var instance = new subtype('jiang', 'student') 
instance.sayname() 

补充:直接使用object.create来实现,其实就是将上面封装的函数拆开,这样演示可以更容易理解。

function supertype(name) { 
 this.name = name 
 this.colors = ['red', 'blue', 'green'] 
} 
supertype.prototype.sayname = function () { 
 console.log(this.name) 
} 
function subtype(name, job) { 
 // 继承属性 
 supertype.call(this, name) 
 this.job = job 
} 
// 继承 
subtype.prototype = object.create(supertype.prototype) 
// 修复constructor 
subtype.prototype.constructor = subtype 
var instance = new subtype('jiang', 'student') 
instance.sayname() 

es6新增了一个方法,object.setprototypeof,可以直接创建关联,而且不用手动添加constructor属性。

// 继承 
object.setprototypeof(subtype.prototype, supertype.prototype) 
console.log(subtype.prototype.constructor === subtype) // true 

以上所述是小编给大家介绍的javascript的六种继承方式(推荐),希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网