javascript - 在Javascript中调用类的其他静态方法中的静态方法
问题描述
我有一个带有一些静态方法的类。我试图在同一类的另一个静态方法中调用我的一个静态方法。
我试图用它来调用它,this.constructor.method();
但它不起作用,因为它在那里没有正确绑定。
如果我想调用其他方法,我也尝试将 this 绑定到该方法。this.constructor.listenerFunc.bind(this.constructor)
. 还是行不通。
代码如下所示:
class MyClass {
constructor() {
};
static firstMethod(){
//do some things
this.constructor.secondMethod();
};
static secondMethod(){
//do other things
};
}
var x = new MyClass;
console.log(x)
解决方案
除了阻止您的示例工作的错字之外,您还可以使用this.
符号和经典的静态调用:
class MyClass {
constructor() {
};
static firstMethod(){
this.secondMethod();
MyClass.secondMethod();
};
static secondMethod(){
console.log('I was called!');
};
}
MyClass.firstMethod();
推荐阅读
- sql - 具有始终加密列的 TSQL 上的 GETDATE() 函数错误
- sas - 在 SAS DATA 步骤中使用 WHERE SAME AND 运算符
- matlab - 每次迭代后循环输出相同的“ev”值
- python - 在Python中按索引删除列表中的多个元素
- javascript - Angular,删除来自数据库的 HTML 标签
- spring - @PostConstruct 和在配置类中使用 new 创建的 bean
- angularjs - 打开引导模式弹出窗口时启用背景以进行编辑和工作
- unity3d - SceneManager 没有激活我之前的场景
- python - 在 python 文件中插入文档字符串属性
- angular - 带有 Angular 组件的 Safari 中的粘性位置