为什么这个闭包没有访问“this”关键字? - jQuery [英] Why doesn't this closure have access to the 'this' keyword? - 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();
它会提醒'undefined'然后'hello'。显然,这不应该是jQuery具体的,但这是我最初的代码我可以想出的最简单的形式。在 do_stuff()
中的闭包可以访问该范围中的变量,但显然这个规则不适用于 this
关键字。
问题:
会发生什么$ c $当
do_stuff()
(在这种情况下 $。get()
)范围之外传递闭包时, ? myThis
是否包含这个
的副本或其引用?在闭包中使用这一般不是个好主意吗?
任何回应非常感激。
当关闭被传递到do_stuff()范围之外(在这种情况下为$)时,会发生什么情况。当我们使用这个方法时,会发生这种情况:
每个函数都有自己的执行上下文,this
关键字检索当前上下文的值。
doStuff
标识符和obj .myMethod
属性指的是同一个函数对象,但是因为你正在调用它作为一个对象的属性(obj.myMethod();
) ,该函数中的this
值将引用obj
。
当Ajax请求成功时,jQuery将调用第二个函数(启动一个新的执行上下文),它将使用包含用于请求的设置的对象作为
this
该回调的值。
myThis是否包含此副本或其引用?
myThis
标识符将包含对象的引用也通过外部范围上的this
值引用。
如果你理解
this
值隐式处理,我没有看到任何问题...
由于您使用jQuery,您可能需要检查
jQuery.proxy
方法,是一种可以使用的实用方法保存函数的上下文,例如:function myObject(){
this.myHello =hello ;
this.myMethod = do_stuff;
}
function do_stuff(){
$ .get('http://example.com',jQuery.proxy(function(){
alert this.myHello);
},this)); //我们绑定外部这个值作为
内的这个值}
var obj = new myObject;
obj.myMethod();
另请参阅:
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 thethis
keyword.Questions:
What happens to
this
when the closure is passed outside the scope ofdo_stuff()
(in this case$.get()
)? DoesmyThis
contain a copy ofthis
or a reference to it? Is it generally not a good idea to usethis
in closures?Any response much appreciated.
解决方案What happens to this when the closure is passed outside the scope of do_stuff() (in this case $.get())?
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.Does myThis contain a copy of this or a reference to it?
The
myThis
identifier will contain a reference to the object that is also referenced by thethis
value on the outer scope.Is it generally not a good idea to use this in closures?
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:function myObject(){ this.myHello = "hello"; this.myMethod = do_stuff; } function do_stuff(){ $.get('http://example.com', jQuery.proxy(function(){ alert(this.myHello); }, this)); // we are binding the outer this value as the this value inside } var obj = new myObject; obj.myMethod();
See also:
这篇关于为什么这个闭包没有访问“this”关键字? - jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!