简体   繁体   English

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

[英]Is it possible to access variables and functions inside static methods of class in ES6?

Firstly the code that will explain my query 首先,将解释我的查询的代码

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));

And, now a codePen example https://codepen.io/hellouniverse/pen/jYyYre 而且,现在是一个codePen示例https://codepen.io/hellouniverse/pen/jYyYre

Now, a disclaimer I have to give. 现在,我不得不放弃一个免责声明。 I have searched through stackoverflow, online and my own experience. 我搜索了stackoverflow,在线和我自己的经验。 I am quite certain it is not possible . 我很确定这是不可能的

If you take the code and run it or check in the codepen, you will notice that myVariable is undefined in static functions. 如果您获取代码并运行它或签入代码笔,则会注意到myVariable在静态函数中未定义。 Also, I can not call a normal function from a static function. 另外,我不能从静态函数调用普通函数。

Am I correct in this statements? 我的陈述正确吗? Or is it possible or has a workaround? 还是可能或有解决方法?

I assume that you want to have accessible primitive data across the static and non-static methods. 我假设您希望通过静态和非静态方法都可以访问原始数据。 For such cases you would rather need to use Symbol or const . 在这种情况下,您宁愿使用Symbolconst

How to use symbol you can read here: MDN Symbol 如何使用符号,您可以在此处阅读: MDN符号

And your modified examples: using Symbol and using the const keyword 以及修改后的示例: 使用Symbol使用const关键字

Looking at your codepen, the answer is no you cannot use this in a static function call, UNLESS you are calling another static method. 查看您的代码笔,答案是否定的,您不能在静态函数调用中使用this ,除非您正在调用另一个静态方法。 As MDN points out, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static , you can have this 正如MDN所指出的那样, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static ,您可以使用它

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

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

You have to understand what a class method desugars to. 您必须了解类方法的适用范围。 Consider 考虑

class Foo {
  static bar () {
    console.log(this.fooProp);
  }
};

Is more-or-less the same as this: 大致相同:

function Foo() {};
Foo.bar = function () { console.log(this.fooProp); };

Note that it isn't Foo.prototype.bar . 请注意,它不是Foo.prototype.bar Functions are Objects in JavaScript, and just like any other object when you call a method of an object (eg Foo.bar() ) then this is set to the object it is called on (eg the function 'Foo', not an instance of Foo). 函数是在JavaScript对象,就像任何其他物体,当你调用对象的方法(如Foo.bar()那么this被设置为它被称为上(如函数“富”的对象,而不是一个实例Foo)。 I can say 我可以说

Foo.fooProp = 3;
Foo.bar(); // logs 3

Without ever actually creating an instance of Foo. 从未实际创建Foo的实例。 I can also call it from an instance 我也可以从实例中调用它

let foo = new Foo();
foo.constructor.bar(); // logs 3

But the this value will always refer to the class/constructor rather than the instance 但是, this值将始终引用类/构造函数而不是实例

foo.fooProp = 5;
foo.constructor.bar(); // still 3

Unless one does something like this: 除非有人这样​​做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM