开发者

Javascript requires making a copy of this?

开发者 https://www.devze.com 2023-03-10 05:40 出处:网络
In order to pass a 开发者_如何学运维value out of the ajax() success callback, I had to do this:

In order to pass a 开发者_如何学运维value out of the ajax() success callback, I had to do this:

var a = this;

And then in success():

a.myresult = result;

Why is it necessary to make a copy of this? When I tried this.myresult, it did not work.


Inside the context of your ajax callback, the this keyword has changed context.

Outside your method, it referred to some object. Inside the callback, it refers to the jQuery object (since the method is invoked in the context of a jQuery object).


Need to see your code, but my guess is you're using "this" in the context of an anonymous function, so "this" refers to the function.


this is only meaningful within context. That context within the object's code.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号