javascript - 如何调用 javascript 对象的根类方法?

标签 javascript oop inheritance super

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()
  }
}

但我收到错误:

Uncaught TypeError: Cannot read property 'walk' of undefined

最佳答案

Cat调用动物的walk()方法,绕过哺乳动物的walk():

class Cat extends Mammal{
  walk(){
     Animal.prototype.walk.call(this)
  }
}

建议者Keithtaha

关于javascript - 如何调用 javascript 对象的根类方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46639676/

相关文章:

JavaScript 图表 API : Flot jQuery Plugin OR Google Visualization?

php - 使用 PHP 序列化维护对象关系

javascript - Controller 中的 $http 请求?

java - 如果我们在实体类的getter和setter中添加逻辑是不是不好的设计

c# - 我应该打破哪种模式?

java - 复制并粘贴到 javafx 应用程序中嵌入的 codemirror.js

javascript - 是否可以确定服务器是否调用了 Meteor 方法

javascript - 在具有转义字符(双引号)的对象上使用 json 编码

python父类 'wrapping'子类方法

Java-套接字继承