严格模式回调中访问公共值

Access common value in call back in strict mode

我在这样的回调中访问了公共值。

function InfoClass() {
    var local = 1;
    self = this;
}
InfoClass.prototype.myFunction = function (){
    this.win.addEventListener('open', function(e){
          // I can't use 'this' here. 
        self.local // So I access common value in callback like this.
    });
}

在严格模式工作之前,

但是,现在我需要设置严格模式,所以它显示错误 self doesn't have var.

还有其他方法吗??

您可以尝试关注

InfoClass.prototype.myFunction = function (){
    var self = this;
    this.win.addEventListener('open', function(e){
        self.local // you can access the function now
    });
}

或者你可以像下面这样使用箭头函数

InfoClass.prototype.myFunction = function (){
    this.win.addEventListener("open", (e) => {
         this.local // you can use this now
    });
}

供参考,Arrow functions