我是 JS 新手,正在学习 JS 类(class),但遇到错误,提示日志未定义。这是代码:
class Keyboard {
log() {
return true;
}
print() {
log() ? "True" : "False";
}
}
const mir = new Keyboard();
mir.print();
最佳答案
您需要this.log()
。我添加了额外的控制台日志,以便您可以正确地看到输出。
class Keyboard {
log() {
return true;
}
print() {
console.log(this.log() ? "True" : "False");
}
}
const mir = new Keyboard();
mir.print();
关于javascript - 为什么我得到日志未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57536655/