众所周知,在es6之前,前端是不存在类的语法糖,所以不能像其他语言一样用extends关键字就搞定继承关系,需要一些额外的方法来实现继承。下面就介绍一些常用的方法,红宝书已经概括的十分全面了,所以本文基本就是对红宝书继承篇章的笔记和梳理。
原型链继承
function parent() {
this.name = 'arzh'
}
parent.prototype.getname = function () {
console.log(this.name)
}
function child() {
}
//主要精髓所在
child.prototype = new parent()
child.prototype.constructor = child
var arzhchild = new child()
arzhchild.getname() // 'arzh'
原型链继承缺点:
1.每个实例对引用类型属性的修改都会被其他的实例共享
function parent() {
this.names = ['arzh','arzh1'];
}
function child() {
}
//主要精髓所在
child.prototype = new parent()
child.prototype.constructor = child
var arzhchild2 = new child()
arzhchild2.names.push('arzh2')
console.log(arzhchild2.names) //[ 'arzh', 'arzh1', 'arzh2' ]
var arzhchild3 = new child()
arzhchild3.names.push('arzh3')
console.log(arzhchild3.names) //[ 'arzh', 'arzh1', 'arzh2', 'arzh3' ]
2.在创建child实例的时候,无法向parent传参。这样就会使child实例没法自定义自己的属性(名字)
借用构造函数(经典继承)
function parent() {
this.names = ['arzh','arzh1']
}
function child() {
parent.call(this)
}
var arzhchild2 = new child()
arzhchild2.names.push('arzh2')
console.log(arzhchild2.names) //[ 'arzh', 'arzh1', 'arzh2' ]
var arzhchild3 = new child()
arzhchild3.names.push('arzh3')
console.log(arzhchild3.names) //[ 'arzh', 'arzh1', 'arzh3' ]
优点:
- 解决了每个实例对引用类型属性的修改都会被其他的实例共享的问题
- 子类可以向父类传参
function parent(name) {
this.name = name
}
function child(name) {
parent.call(this, name)
}
var arzhchild = new child('arzh');
console.log(arzhchild.name); // arzh
var arzhchild1 = new child('arzh1');
console.log(arzhchild1.name); // arzh1
缺点:
- 无法复用父类的公共函数
- 每次子类构造实例都得执行一次父类函数
组合式继承(原型链继承和借用构造函数合并)
function parent(name) {
this.name = name
this.body = ['foot','hand']
}
function child(name, age) {
parent.call(this, name)
this.age = age
}
child.prototype = new parent()
child.prototype.constructor = child
var arzhchild1 = new child('arzh1', '18')
arzhchild1.body.push('head1')
console.log(arzhchild1.name,arzhchild1.age) //arzh1 18
console.log(arzhchild1.body) //[ 'foot', 'hand', 'head1' ]
var arzhchild2 = new child('arzh2', '20')
arzhchild2.body.push('head2')
console.log(arzhchild2.name,arzhchild2.age) //arzh2 20
console.log(arzhchild2.body) //[ 'foot', 'hand', 'head2' ]
优点:
- 解决了每个实例对引用类型属性的修改都会被其他的实例共享的问题
- 子类可以向父类传参
- 可实现父类方法复用
缺点:
- 需执行两次父类构造函数,第一次是
child.prototype = new parent(),第二次是parent.call(this, name)造成不必要的浪费
原型式继承
复制传入的对象到创建对象的原型上,从而实现继承
function createobj(o) {
function f(){}
f.prototype = o;
return new f();
}
var person = {
name : 'arzh',
body : ['foot','hand']
}
var person1 = createobj(person)
var person2 = createobj(person)
console.log(person1) //arzh
person1.body.push('head')
console.log(person2) //[ 'foot', 'hand', 'head' ]
缺点: 同原型链继承一样,每个实例对引用类型属性的修改都会被其他的实例共享
寄生式继承
我们可以使用object.create来代替上述createobj的实现,原理基本上是一样的。寄生式继承其实就是在createobj的内部以某种形式来增强对象(这里的增强可以理解为添加对象的方法),最后返回增强之后的对象。
function createenhanceobj(o) {
//代替原型式继承的createobj
var clone = object.create(o)
clone.getname = function () {
console.log('arzh')
}
return clone;
}
通过createenhanceobj就可以在创建对象的时候,把对象方法也通过此种方式继承。
缺点: 同借用构造函数一样,无法复用父类函数,每次创建对象都会创建一遍方法
寄生组合式继承
不需要为了子类的原型而多new了一次父类的构造函数,如child.prototype = new parent() 只需要复制父类原型的一个副本给子类原型即可
function inheritprototype(parent, child){
child.prototype = object.create(parent.prototype) //创建父类原型的一个副本,把副本赋值给子类原型
child.prototype.constructor = child;
}
function parent(name) {
this.name = name
}
parent.prototype.getname = function () {
console.log(this.name)
}
function child(color) {
parent.call(this, 'arzh')
this.color = color
}
inheritprototype(parent, child)
var arzhchild = new child('red')
console.log(arzhchild.name) // 'arzh'
优点: 不必为了指定子类型的原型而调用父类型的构造函数
es6继承
es6支持通过类来实现继承,方法比较简单,代码如下
class point {
constructor(x, y) {
this.x = x
this.y = y
}
tostring() {
return this.x + '' + this.y
}
}
class colorpoint extends point {
constructor(x, y, color) {
super(x, y) //调用父类的constructor(x, y)
this.color = color
}
tostring() {
return this.color + ' ' + super.tostring() // 调用父类的tostring()
}
}
var colorpoint = new colorpoint('1', '2', 'red')
console.log(colorpoint.tostring()) // red 12
总结
本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注代码网的更多内容!
发表评论