【发布时间】:2018-03-20 06:19:15
【问题描述】:
class Animal{
constructor(){...}
walk(){ console.log('Animal walking...')}
}
class Mammal extends Animal{
constructor(){...}
walk(){console.log('Mammal walking...')}
}
class Cat extends Mammal{
constructor(){}
walk(){console.log('Cat walking...')}
}
在上面的类层次结构中,我想从 Cat 对象调用 Animal 的 walk() 实现
我试过了:
class Cat extends Mammal{
constructor(){}
walk(){
super.super.walk()
}
}
但我得到了错误:
未捕获的类型错误:无法读取未定义的属性“walk”
【问题讨论】:
-
你可以试试 ->
Animal.prototype.walk.call(c) -
Animal.prototype.walk()如果你想在子类的上下文中调用它Animal.prototype.walk.call(c) -
@Keith ,塔哈:那行得通。非常感谢
标签: javascript oop inheritance super