我在一个文件中有一个类,其中包含另一个js文件中调用的另一个静态函数。在同一个文件中的另一个帮助函数中的类中调用静态函数
module.export = class myClass{
static create(){
...
}
}
// helpers
function callCreate(){
..
}
我要打电话的myClass
静态函数callCreate
辅助函数。我怎样才能做到这一点?
class MyClass {
property() {
console.log('i am normal member');
}
static func() {
console.log('i am static member');
}
static funcThis() {
console.log('i am static member');
console.log(this === MyClass); // true
this.func(); // will run fine as a static member of a class
this.property(); // will give error as a normal member of a class
}
}
(new MyClass()).property();
MyClass.func();
MyClass.funcThis();
静态成员直接由类名来访问,并且不与对象连接:
该类的静态成员的访问方式如下:'Class.staticVar'。在你的情况下,它将是'myClass.create'。 – RaghavGarg