“这个”闭包内的关键字
我看过很多例子,但似乎无法让一些示例代码起作用。
采用以下代码:
var test = (function(){
var t = "test";
return {
alertT: function(){
alert(t);
}
}
}());
我在 window.load 上有一个函数,如下所示:
test.alertT();
一切正常。但是,当我尝试在 alertT 中的 alert() 中显式设置 t 的上下文时,我只是得到了未定义的结果。
我已经尝试过:
var that = this;
alert(that.t); //undefined
我已经尝试过:
return {
that: this,
alertT: function(){
alert(that.t); // undefined!
}
}
我已经尝试过:
var test = (function(){
var t = "test";
var myObj = this;
return {
alertT: function(){
alert(myObj.t); // undefined!
}
}
}());
我错过了什么?我需要能够为回调等显式设置上下文。我也看到过一些示例(http://stackoverflow.com/questions/346015/javascript-closures-and-this-context),它们看起来像我的正在做,那么为什么这不起作用呢?
I've seen a bunch of examples but can't seem to get some sample code to work.
Take the following code:
var test = (function(){
var t = "test";
return {
alertT: function(){
alert(t);
}
}
}());
and I have a function on window.load like:
test.alertT();
That all works fine. However, when I try to explicitly set the context of t inside the alert() in alertT, I just get undefined.
I've tried:
var that = this;
alert(that.t); //undefined
I've tried:
return {
that: this,
alertT: function(){
alert(that.t); // undefined!
}
}
and I've tried:
var test = (function(){
var t = "test";
var myObj = this;
return {
alertT: function(){
alert(myObj.t); // undefined!
}
}
}());
what am I missing? I need to be able to set the context explicitly for things like callbacks etc. I've seen examples too (http://stackoverflow.com/questions/346015/javascript-closures-and-this-context) that seem like what I'm doing, so why does this not work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
t
只是外部匿名函数(因此也是内部匿名函数)范围内的普通变量。它不是对象的属性,因此您只需设置它,而无需引用this
、that
或the_other
。您使用的语法是创建类似于 JS 中的私有变量的内容的常用模式。
t
is just a normal variable in the scope of the outside anonymous function (and thus also the inner anonymous function). It isn't a property on an object, so you simply set it without reference tothis
,that
, orthe_other
.The syntax you are using is the usual pattern for creating something that acts like a private variable in JS.
t 不在“this”的范围内。 t 是该方法的局部变量。所以你需要在某个地方做
...
这是一个来自应用程序的真实示例,我正在编写
forEach 函数内的范围是我正在迭代的数组“单元格”的范围。由于我想在调用范围上执行操作,因此我使用了闭包...
t is not on the scope of 'this'. t is a variable local to the method. So somewhere you need to do
...
here is a real life example from an app I am writing
the scope inside the forEach function is the scope of the array 'cells' over which I am iterating. Since I want to do things on the calling scope, I use a closure...
在 C# 和 Java 中,可以执行如下操作:
变量 a 和 b 将具有来自不同 x 的值,因为 1 是类的 x,1 是方法 x。
现在,想象一下如果您无法使用
this
显式引用类的 x.然后你必须执行以下操作:这就是你在 JavaScript 中遇到的情况,差不多。至少在你描述的情况下是这样。通过使用
apply
和call
方法,您可以更改函数执行的上下文,但您永远无法区分具有相同名称但不同作用域的变量。您只需为此使用不同的名称即可。In C# and Java, one can do something like this:
The variables a and b will have values from different x's, since one is the class's x an one is the methods x.
Now, imagine if you could not use
this
to explicitly refer to the the class's x. Then you'd have to do the following:That i the situation you have in JavaScript, pretty much. At least in the situation you're describing. By using the methods
apply
andcall
you can change the context that a function is executed in, but you can never distinguish variables with the sames names but different scopes. You'll simply have to use different names for that.