如何实现类的继承?
web吧
全部回复
仅看楼主
level 3
类的继承在几年前是重点内容,有n种继承⽅式各有优劣,es6普及后越来越不重要,那么多种写法有点『回字有四样写法』的意思,如果还想深入理解去看红宝书即可,我们目前只实现⼀种最理想的继承⽅式。
2021年01月08日 09点01分 1
level 3
function Parent(name)
{ this.parent = name
}
Parent.prototype.say = function {
console.log(`${this.parent}: 你打篮球的样⼦像kunkun`)
}
function Child(name, parent) {
// 将父类的构造函数绑定在子类上
2021年01月08日 09点01分 2
level 3
Parent.call(this, parent)
this.child = name
}
/**
1. 这⼀步不用Child.prototype =Parent.prototype的原因是怕共享内存,修改父类原型对象就会影响子类;
2. 不⽤Child.prototype = new Parent的原因是会调用2次父类的构造方法(另⼀次是call),会存在⼀份多余的父类实例属性;
3. Object.create是创建了父类原型的副本,与父类原型完全隔离
*/
2021年01月08日 09点01分 3
level 3
Child.prototype = Object.create(Parent.prototype);
Child.prototype.say = function {
console.log(`${this.parent}好,我是练习时长两年半的${this.child}`);
}
2021年01月08日 09点01分 4
level 3
// 注意记得把子类的构造指向子类本身
Child.prototype.constructor = Child;
var parent = new Parent('father');
parent.say // father: 你打篮球的样子像kunkun
var child = new Child('cxk', 'father');
child.say // father好,我是练习时长两年半的cxk
2021年01月08日 10点01分 5
1