为什么这个闭包不能访问“this”?关键词? - jQuery
我是闭包(以及一般的 Javscript)的初学者,并且我无法找到关于此代码中发生的情况的令人满意的解释:
function myObject(){
this.myHello = "hello";
this.myMethod = do_stuff;
}
function do_stuff(){
var myThis = this;
$.get('http://example.com', function(){
alert(this.myHello);
alert(myThis.myHello);
});
}
var obj = new myObject;
obj.myMethod();
它将警告“未定义”,然后警告“你好”。显然这不应该是 jQuery 特有的,但这是我能想到的原始代码的最简单形式。 do_stuff() 中的闭包可以访问该范围内的变量,但显然此规则不适用于 this 关键字。
问题:
当闭包传递到 do_stuff()
范围之外时(在本例中为 $.get()
),this
会发生什么? myThis
是否包含 this
的副本或对其的引用?在闭包中使用 this
通常不是一个好主意吗?
非常感谢任何回应。
I'm a beginner to closures (and Javscript in general), and I can't find a satisfactory explanation as to what's going on in this code:
function myObject(){
this.myHello = "hello";
this.myMethod = do_stuff;
}
function do_stuff(){
var myThis = this;
$.get('http://example.com', function(){
alert(this.myHello);
alert(myThis.myHello);
});
}
var obj = new myObject;
obj.myMethod();
It will alert 'undefined' and then 'hello'. Obviously this should not be jQuery specific, but this is the simplest form of my original code I could come up with. The closure in do_stuff()
has access to the variables in that scope, but apparently this rule does not apply to the this
keyword.
Questions:
What happens to this
when the closure is passed outside the scope of do_stuff()
(in this case $.get()
)? Does myThis
contain a copy of this
or a reference to it? Is it generally not a good idea to use this
in closures?
Any response much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每个函数都有自己的执行上下文,
this
关键字检索当前上下文的值。doStuff
标识符和obj.myMethod
属性引用同一个函数对象,但由于您将其作为对象的属性调用 (obj.myMethod( );
),该函数内的this
值将引用obj
。当 Ajax 请求成功时,jQuery 将调用第二个函数(启动新的执行上下文),并且它将使用一个包含用于请求的设置的对象作为该回调的
this
值。myThis
标识符将包含对对象的引用,该对象也由外部作用域上的this
值引用。如果您了解如何隐式处理
this
值,我看不出有任何问题...由于您使用的是 jQuery,您可能需要检查
jQuery.proxy
方法,是一种实用方法,可用于保留函数的上下文,例如示例:另请参阅:
Each function has its own execution context, the
this
keyword retrieves the value of the current context.The
doStuff
identifier and theobj.myMethod
property refer to the same function object, but since you are invoking it as a property of an object (obj.myMethod();
), thethis
value inside that function, will refer toobj
.When the Ajax request has succeeded, jQuery will invoke the second function (starting a new execution context), and it will use an object that contains the settings used for the request as the
this
value of that callback.The
myThis
identifier will contain a reference to the object that is also referenced by thethis
value on the outer scope.If you understand how the
this
value is handled implicitly, I don't see any problem...Since you are using jQuery, you might want to check the
jQuery.proxy
method, is an utility method that can be used to preserve the context of a function, for example:See also:
注意匿名函数。该范围内的 this 是该函数的范围。 myThis 是外部作用域的 this,其中定义了 myHello。在萤火虫中检查一下。
我相信,“this”总是指的是当前的执行范围。如果您想获取当前范围并保留它,您可以执行您所做的操作,即将其分配给另一个变量。
note the anonymous function. this in that scope is the scope of the function. myThis is the this of the outer scope, where the myHello has been defined. Check it out in firebug.
'this' always refers to the current scope of execution, i believe. If you want to take the current scope and preserve it, you do what you did, which is assign this to another variable.
它什么也没有“发生”,对于那个闭包来说,这仍然是这个,从闭包调用的函数的执行上下文不会自动继承
this
。所有非标量赋值都是 JavaScript 中的引用。所以它是对
this
的引用,如果您更改其中一个的属性,它们都会更改。在闭包中使用
this
通常是一个好主意,但如果您要在需要访问相同this
的内部使用闭包,那么最好的做法是正是您所做的:var someName = this;
,然后使用someName
访问Nothing "happens" to it, this is still this for that closure, the execution context of the functions called from the closure do not automatically inherit
this
.All non-scalar assignments are references in JavaScript. So it is a reference to
this
, if you change properties on either, they change for both.It is generally a good idea to use
this
in closures, but if you're going to be using closures inside that need to access the samethis
, its good practice to do exactly what you did:var someName = this;
and then access usingsomeName