javascript - javascript:调用基类函数
问题描述
我有以下代码,我正在尝试从基类继承。为什么代码说identify()
没有定义?它不应该从基类调用函数吗?
错误:ReferenceError:标识未定义 source1.js:23:9
class TestBase {
constructor() {
this.type = "TestBase";
}
run() {
console.log("TestBase Run");
}
identify() {
console.log("Identify:" + this.type);
}
}
class DerivedBase extends TestBase {
constructor() {
super();
this.type = "DerivedBase";
}
run() {
console.log("DerivedBase Run");
identify();
}
}
window.onload = function() {
let derived = new DerivedBase();
derived.run();
}
解决方案
this
在调用函数之前添加identify()
;
run() {
console.log("DerivedBase Run");
this.identify();
}
推荐阅读
- git - 如何使用 VS Code 将新文件添加到现有存储库
- python - 当我尝试在 setting.py 中超过 SPECTACULAR_SETTINGS 时,我正在为我的 Django rest api 文档使用 drf-spectacular
- java - 骑士到达目的地的最小步数
- json - 仅使用选定的键将 json 转换为 csv
- c - 64 位计算机如何更改内存中的一个字节?
- clojure - Clojure 的 assoc-in 在其他 lisps 中的名称是什么?
- android-permissions - Android 11:权限对话框不再是模态的。这是设计使然吗?
- python - 如何在 Python 中读取 Excel 中的特定单元格并使其成为变量
- javascript - 从 PHP 文件中的 dropzone.js 检索图像
- visual-studio - 为 Visual Studio 设置正确的环境路径变量