gpt4 book ai didi

javascript - 无法在自己的类中调用静态函数

转载 作者:行者123 更新时间:2023-11-30 09:30:23 25 4
gpt4 key购买 nike

如何在类本身内部调用静态函数?我尝试使用 self 关键字而不是 this,但我仍然遇到错误。

class Test {
static staticFunction() {
console.log('Inside static function.');
}
regularFunction() {
this.staticFunction();
}
}

let test = new Test();
test.regularFunction();

最佳答案

你可以通过类名来引用静态函数,像这样:

class Test {
static staticFunction(input) {
console.log('Inside static function.');
}
regularFunction() {
Test.staticFunction();
}
}

let test = new Test();
test.regularFunction();

关于javascript - 无法在自己的类中调用静态函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46622362/

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