如何从回调中访问此类成员?

发布于 2024-10-10 04:16:16 字数 461 浏览 3 评论 0原文

这个问题最好用一些代码来解释,所以这里是:

// a class
function a_class {
    this.a_var      = null;
    this.a_function = a_class_a_function;
}

// a_class::a_function
function a_class_a_function() {
    AFunctionThatTakesACallback(function() {
        // How to access this.a_var?
    });
}

// An instance
var instance = new a_class();
instance.a_function();

AFunctionThatTakesACallback() 的回调中,如何访问 this.a_var

This question is best explained with some code, so here it is:

// a class
function a_class {
    this.a_var      = null;
    this.a_function = a_class_a_function;
}

// a_class::a_function
function a_class_a_function() {
    AFunctionThatTakesACallback(function() {
        // How to access this.a_var?
    });
}

// An instance
var instance = new a_class();
instance.a_function();

From within the callback in AFunctionThatTakesACallback(), how does one access this.a_var?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗恋未遂 2024-10-17 04:16:16

您需要通过创建引用它的局部变量来扩展 this 的范围,如下所示:

function a_class_a_function() {
   var self = this;
   AFunctionThatTakesACallback(function() {
      console.log(self.a_var); 
   });
}

您需要这样做的原因是因为 this 中的引用AFunctionThatTakesACallback 函数与当前对象的 this 不同,它可能会引用全局 window 对象。 (通常不是你想要的)。

哦,我有没有提到这称为 closure< /强>

You'll need to expand the scope of this by creating a local variable that references it, like this:

function a_class_a_function() {
   var self = this;
   AFunctionThatTakesACallback(function() {
      console.log(self.a_var); 
   });
}

The reason why you need to do this is because the this reference within the AFunctionThatTakesACallback function is not the same this as the current object, it will likely reference the global windowobject instead. (usually not what you want).

Oh, did I mention that this is called a closure?

指尖微凉心微凉 2024-10-17 04:16:16

您可以尝试使用函数对象的 call 方法,该方法可以让您为此指定一个值:

myFunction.call(this, args...)

但我认为在这种情况下,将“this”作为参数之一传递给回调可能会更直接。

You could try using the call method of function objects, which lets you specify a value for this:

myFunction.call(this, args...)

But I think that in this case it would probably be more straightforward to pass 'this' in as one of the parameters to the callback.

渡你暖光 2024-10-17 04:16:16

当您调用 instance.a_function() 时,您实际上是在使用 instance 作为 this 调用 a_class_a_function,因此您可以像这样修改 a_class_a_function

function a_class_a_function() {
    var self = this;
    AFunctionThatTakesACallback(function() {
        // do something with self.a_var
    });
}

这里的问题是,如果您尝试调用 a_class_a_function 而不从实例调用它,那么 this 可能会引用到全局对象window

When you call instance.a_function(), you're really calling a_class_a_function with instance as this, so you can modify a_class_a_function like so:

function a_class_a_function() {
    var self = this;
    AFunctionThatTakesACallback(function() {
        // do something with self.a_var
    });
}

The problem here is that if you attempt to call a_class_a_function without calling it from an instance, then this will likely refer to the global object, window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文