Javascript 是否可以访问ES6中类的静态方法中的变量和函数?

Javascript 是否可以访问ES6中类的静态方法中的变量和函数?,javascript,ecmascript-6,Javascript,Ecmascript 6,首先是解释我的查询的代码 class myClass { constructor() { this.myVariable = 10; } myFunctionCalledFromStaticFunction() { console.log (`myFunctionCalledFromStaticFunction myvariable is ${this.myVariable}`); } static staticMyF

首先是解释我的查询的代码

class myClass {
    constructor() {
        this.myVariable = 10; 
    }

    myFunctionCalledFromStaticFunction() {
        console.log (`myFunctionCalledFromStaticFunction myvariable is ${this.myVariable}`);
    }

    static staticMyFunctionTwo() {
        console.log (`staticMyFunctionTwo myvariable is ${this.myVariable}`);
        this.myFunctionCalledFromStaticFunction();
    }

    static staticMyFunction() {
        console.log (`staticMyFunction myvariable is ${this.myVariable}`);
        myClass.staticMyFunctionTwo();
    }

    myFunctionTwo() {
        console.log (`myFunctionTwo myvariable is ${this.myVariable}`)
        myClass.staticMyFunction();
    }

    myFunction() {
        console.log (`myFunction myvariable is ${this.myVariable}`)
        this.myFunctionTwo();
    }
}

(function ($) {
    'use strict';
         const helloClass = new myClass();
         console.log (`main myvariable is ${helloClass.myVariable}`)
       helloClass.myFunction();  
       myClass.staticMyFunctionTwo();
}(jQuery));
现在是一个代码笔示例

现在,我必须给出一个免责声明我通过stackoverflow、在线和我自己的经验进行了搜索。我很肯定这是不可能的

如果您获取代码并运行它或签入codepen,您将注意到myVariable在静态函数中未定义。另外,我不能从静态函数调用普通函数


我说的对吗?或者它是可能的还是有解决方法?

查看您的代码笔,答案是否定的,您不能在静态函数调用中使用此,除非您正在调用另一个静态方法。正如MDN指出的,您可以

class Logger {
    static error(message) {
        this.log(message, 'error');
    }

    static log(message, level) {
        console[level](message);
    }
}

我假设您希望在静态和非静态方法之间具有可访问的基本数据。在这种情况下,您需要使用
Symbol
const

如何使用符号您可以在此处阅读:


还有修改后的示例:

您必须了解类方法的作用。考虑

class Foo {
  static bar () {
    console.log(this.fooProp);
  }
};
与此大致相同:

function Foo() {};
Foo.bar = function () { console.log(this.fooProp); };
请注意,它不是
Foo.prototype.bar
。函数是JavaScript中的对象,与调用对象的方法(例如
Foo.bar()
)时的任何其他对象一样,然后
将此
设置为它所调用的对象(例如函数“Foo”,而不是Foo的实例)。我可以说

Foo.fooProp = 3;
Foo.bar(); // logs 3
没有实际创建Foo的实例。我也可以从一个实例调用它

let foo = new Foo();
foo.constructor.bar(); // logs 3
但是
这个
值总是引用类/构造函数而不是实例

foo.fooProp = 5;
foo.constructor.bar(); // still 3
除非有人这样做:

Foo.bar.call(foo); // now it's 5

不指向类的实例,因此您无法知道仅在实例中可用的变量的值。同一类可以重复使用100次(实例)。每次myVariable都可以有不同的值。静态变量在所有实例中必须始终相同,因此静态方法不可能使用非静态变量,因为它可以同时是不同的值。因此,您需要的是静态变量,这是可能的,但您需要在JavaScript中找到一个解决方案(超出了这个问题的范围),这里是一个健全性检查。假设
myVariable
不是常数,而是取决于实例,如
this.myVariable=Math.random()
,那么在静态方法中,您希望它等于什么?无法访问。所以这在JavaScript术语中是未定义的。它的实际值应该与您如何编写程序无关。在我看来,这有点误导。This
的值取决于函数的调用方式。当然,如果它被称为
Class.method()
,那么
这将引用构造函数本身。但是它也可以被称为
Class.method.call(obj)
(我不是说这是一个好方法),而
this
将引用
obj
。说“您不能使用
这个
”是错误的。请记住,您永远不应该让
类只使用静态方法。对于该用例,只需使用对象文字即可。@FelixKing yes如果您的语句正确,则始终可以使用call、apply、blind显式设置
this
上下文,但使用
new
关键字创建的对象除外。尽管myClass.staticMyFunction.call(new myClass()),我还是请求您不要这样做;