gpt4 book ai didi

javascript:调用基类函数

转载 作者:行者123 更新时间:2023-11-28 11:45:20 24 4
gpt4 key购买 nike

我有以下代码,并且我正在尝试从基类继承。为什么代码显示 identify() 未定义?它不应该从基类调用该函数吗?

Error: ReferenceError: identify is not defined 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();
}

最佳答案

在调用函数identify()之前添加this

run() {
console.log("DerivedBase Run");
this.identify();
}

关于javascript:调用基类函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51500863/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com