js的继承方式

js的继承方式

一、原型链继承

原型链继承的原理很简单,直接让子类的原型对象指向父类实例,
当子类实例找不到对应的属性和方法时,就会往它的原型对象,
也就是父类实例上找,从而实现对父类的属性和方法的继承
// 父类
function Parent() {
    this.name = '写代码像蔡徐抻'
}

// 父类的原型方法
Parent.prototype.getName = function() {

    return this.name

}

// 子类
function Child() {}

// 让子类的原型对象指向父类实例, 这样一来在Child实例中找不到的属性和方法就会到原型对象(父类实例)上寻找
Child.prototype = new Parent()

/* 根据原型链的规则,顺便绑定一下constructor,这一步不影响继承, 只是在用到constructor时会需要*/
Child.prototype.constructor = Child 


// 然后Child实例就能访问到父类及其原型上的name属性和getName()方法

const child = new Child()

child.name          // '写代码像蔡徐抻'

child.getName()     // '写代码像蔡徐抻'

原型继承的缺点:

1. 由于所有Child实例原型都指向同一个Parent实例,
因此对某个Child实例的父类引用类型变量修改会影响所有的Child实例 
2. 在创建子类实例时无法向父类构造传参, 即没有实现 super() 的功能
// 示例:
function Parent() {
    this.name = ['写代码像蔡徐抻'] 
}

Parent.prototype.getName = function() {
    return this.name
}

function Child() {}
Child.prototype = new Parent() Child.prototype.constructor = Child

// 测试 
const child1 = new Child() 
const child2 = new Child() 
child1.name[0] = 'foo' 
console.log(child1.name) // ['foo'] 
console.log(child2.name) 
// ['foo'] 
//(预期是['写代码像蔡徐抻'], 对child1.name的修改引起了所有child实例的变化)

二. 构造函数继承

构造函数继承,即在子类的构造函数中执行父类的构造函数,
并为其绑定子类的`this`,让父类的构造函数把成员属性和方法都挂到`子类的this`上去,
这样既能避免实例之间共享一个原型实例,又能向父类构造方法传参

function Parent(name) {

    this.name = [name]

}

Parent.prototype.getName = function() {

    return this.name

}

function Child() {

    Parent.call(this, 'zhangsan')   // 执行父类构造方法并绑定子类的this, 使得父类中的属性能够赋到子类的this上

}

//测试

const child1 = new Child()

const child2 = new Child()

child1.name[0] = 'foo'

console.log(child1.name)          // ['foo']

console.log(child2.name)          // ['zhangsan']

child2.getName()  // 报错,找不到getName(), 构造函数继承的方式继承不到父类原型上的属性和方法      

构造函数继承的缺点:

1. 继承不到父类原型上的属性和方法

三. 组合式继承

既然原型链继承和构造函数继承各有互补的优缺点,
那么我们为什么不组合起来使用呢, 所以就有了综合二者的组合式继承
function Parent(name) {
    this.name = [name]
}

Parent.prototype.getName = function() {
    return this.name
}

function Child() {
    // 构造函数继承
    Parent.call(this, 'zhangsan') 
}

//原型链继承
Child.prototype = new Parent()
Child.prototype.constructor = Child


//测试

const child1 = new Child()

const child2 = new Child()

child1.name[0] = 'foo'

console.log(child1.name)          // ['foo']

console.log(child2.name)          // ['zhangsan']

child2.getName()                  // ['zhangsan']

组合式继承的缺点:

每次创建子类实例都执行了两次构造函数( Parent.call() 和 new Parent() ),
虽然这并不影响对父类的继承,
但子类创建实例时,原型中会存在两份相同的属性和方法,这并不优雅

四. 寄生式组合继承

为了解决构造函数被执行两次的问题, 我们将 指向父类实例 
改为 指向父类原型, 减去一次构造函数的执行
function Parent(name) {
    this.name = [name]
}

Parent.prototype.getName = function() {
    return this.name
}

function Child() {
    // 构造函数继承
    Parent.call(this, 'zhangsan') 

}

//原型链继承

// Child.prototype = new Parent()

Child.prototype = Parent.prototype  //将`指向父类实例`改为`指向父类原型`

Child.prototype.constructor = Child

//测试
const child1 = new Child()

const child2 = new Child()

child1.name[0] = 'foo'

console.log(child1.name)          // ['foo']

console.log(child2.name)          // ['zhangsan']

child2.getName()                  // ['zhangsan']
但这种方式存在一个问题,由于子类原型和父类原型指向同一个对象,
我们对子类原型的操作会影响到父类原型,
例如给 Child.prototype 增加一个getName()方法,
那么会导致 Parent.prototype 也增加或被覆盖一个getName()方法,
为了解决这个问题,我们给 Parent.prototype 做一个浅拷贝
function Parent(name) {
    this.name = [name]
}

Parent.prototype.getName = function() {
    return this.name
}

function Child() {
    // 构造函数继承
    Parent.call(this, 'zhangsan') 
}

//原型链继承

// Child.prototype = new Parent()

Child.prototype = Object.create(Parent.prototype)  //将`指向父类实例`改为`指向父类原型`

Child.prototype.constructor = Child

//测试

const child = new Child()

const parent = new Parent()

child.getName()                  // ['zhangsan']

parent.getName()                 // 报错, 找不到getName()
到这里我们就完成了ES5环境下的继承的实现,这种继承方式称为 寄生组合式继承 ,是目前最成熟的继承方式,babel对ES6继承的转化也是使用了寄生组合式继承
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容