53. 用es5实现`extends`
分享给更多人
中等难度 -通过 / -执行
相信你一定用过 extends 关键字。
你能否实现一个ES5的myExtends()
函数来模仿 extends
?
myExtends()
接受一个SubType 和 SuperType,然后返回一个新的type。
const InheritedSubType = myExtends(SuperType, SubType)
const instance = new InheritedSubType()
// 上述代码需要(几乎)和下面的一样
class SubType extends SuperType {}
const instance = new SubType()
为了完成该题目,你需要完全掌握JavaScript中的继承
注意
BFE.dev会用下面的SuperType和 SubType来测试你的代码。
function SuperType(name) {
this.name = name
this.forSuper = [1, 2]
this.from = 'super'
}
SuperType.prototype.superMethod = function() {}
SuperType.prototype.method = function() {}
SuperType.staticSuper = 'staticSuper'
function SubType(name) {
this.name = name
this.forSub = [3, 4]
this.from = 'sub'
}
SubType.prototype.subMethod = function() {}
SubType.prototype.method = function() {}
SubType.staticSub = 'staticSub'