访问"this"从其他函数输入JavaScript变量 [英] Accessing "this" Type JavaScript Variables From Other Functions

查看:110
本文介绍了访问"this"从其他函数输入JavaScript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个事件触发,即使它在我试图从中访问变量的函数内部,也得到了Uncaught TypeError: Cannot read property '...' of undefined.所以,让我们说:

I have an event firing and even though it's inside of the function from which I'm trying to access variables, I get Uncaught TypeError: Cannot read property '...' of undefined. So, let's say:

( function($) {

    $.fn.main = function() {

        this.setting = 1;

        $("#someElement").scroll( function() {

            console.debug(this.setting);

        } );

    }

} )(jQuery);

我确定这与计时有关,但话又说回来,我可能是错的.我应该复制this并将其公开吗?任何人?谢谢.

I'm sure it has something to do with timing, but then again, I could be wrong. Should I make a copy of this and make that public? Anyone? Thanks.

推荐答案

由于this是动态获取其值,因此无法将this的值固定在闭包中.

The value of this cannot be pinned in a closure as the this gets its value dynamically.

尝试:

var self = this;

并提及自己.

这篇关于访问"this"从其他函数输入JavaScript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆