为什么这个闭包不能访问“this”?关键词? - jQuery

发布于 2024-09-11 13:07:08 字数 699 浏览 5 评论 0原文

我是闭包(以及一般的 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 技术交流群。

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

发布评论

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

评论(3

半葬歌 2024-09-18 13:07:08

当闭包传递到 do_stuff() 范围之外(在本例中为 $.get())时会发生什么?

每个函数都有自己的执行上下文,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)); // we are binding the outer this value as the this value inside
}

var obj = new myObject;
obj.myMethod();

另请参阅:

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 the obj.myMethod property refer to the same function object, but since you are invoking it as a property of an object (obj.myMethod();), the this value inside that function, will refer to obj.

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 the this 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:

泪痕残 2024-09-18 13:07:08
$.get('http://example.com', function(){
    alert(this.myHello);  // this is scoped to the function
    alert(myThis.myHello);  // myThis is 'closed-in'; defined outside
});

注意匿名函数。该范围内的 this 是该函数的范围。 myThis 是外部作用域的 this,其中定义了 myHello。在萤火虫中检查一下。

我相信,“this”总是指的是当前的执行范围。如果您想获取当前范围并保留它,您可以执行您所做的操作,即将其分配给另一个变量。

$.get('http://example.com', function(){
    alert(this.myHello);  // this is scoped to the function
    alert(myThis.myHello);  // myThis is 'closed-in'; defined outside
});

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.

放我走吧 2024-09-18 13:07:08
$.get('http://example.com', function(){
   // inside jQuery ajax functions - this == the options used for the ajax call
});

当闭包传递到 do_stuff() 范围之外(在本例中为 $.get())时,会发生什么情况?

它什么也没有“发生”,对于那个闭包来说,这仍然是这个,从闭包调用的函数的执行上下文不会自动继承this

myThis 是否包含此副本或对其的引用?

所有非标量赋值都是 JavaScript 中的引用。所以它是对 this 的引用,如果您更改其中一个的属性,它们都会更改。

在闭包中使用它通常不是一个好主意吗?

在闭包中使用 this 通常是一个好主意,但如果您要在需要访问相同 this 的内部使用闭包,那么最好的做法是正是您所做的:var someName = this;,然后使用someName访问

$.get('http://example.com', function(){
   // inside jQuery ajax functions - this == the options used for the ajax call
});

What happens to this when the closure is passed outside the scope of do_stuff() (in this case $.get())?

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.

Does myThis contain a copy of this or a reference to it?

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.

Is it generally not a good idea to use this in closures?

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 same this, its good practice to do exactly what you did: var someName = this; and then access using someName

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