“这个”闭包内的关键字

发布于 2024-09-26 16:37:54 字数 1007 浏览 3 评论 0原文

我看过很多例子,但似乎无法让一些示例代码起作用。

采用以下代码:

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 技术交流群。

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

发布评论

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

评论(3

十二 2024-10-03 16:37:54

t 只是外部匿名函数(因此也是内部匿名函数)范围内的普通变量。它不是对象的属性,因此您只需设置它,而无需引用 thisthatthe_other

var test = (function(){
    var t = "test";
    return {
        alertT: function(){ 
            alert(t);
        },
        setT: function (new_value) {
            t = new_value;
        }
    }
}());
test.alertT();
test.setT('hello, world');
test.alertT();

您使用的语法是创建类似于 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 to this, that, or the_other.

var test = (function(){
    var t = "test";
    return {
        alertT: function(){ 
            alert(t);
        },
        setT: function (new_value) {
            t = new_value;
        }
    }
}());
test.alertT();
test.setT('hello, world');
test.alertT();

The syntax you are using is the usual pattern for creating something that acts like a private variable in JS.

黯然 2024-10-03 16:37:54

t 不在“this”的范围内。 t 是该方法的局部变量。所以你需要在某个地方做

this.t = whatever

...

这是一个来自应用程序的真实示例,我正在编写

var scope = this;

cells.forEach(function(cell, index) {
            var given = cell.get('given');

            var value = cell.get('value'),
                            valueAsString = '%@'.fmt(value);


             var rowValues = scope.getRowForIndex(index);
            ...
}

forEach 函数内的范围是我正在迭代的数组“单元格”的范围。由于我想在调用范围上执行操作,因此我使用了闭包...

t is not on the scope of 'this'. t is a variable local to the method. So somewhere you need to do

this.t = whatever

...

here is a real life example from an app I am writing

var scope = this;

cells.forEach(function(cell, index) {
            var given = cell.get('given');

            var value = cell.get('value'),
                            valueAsString = '%@'.fmt(value);


             var rowValues = scope.getRowForIndex(index);
            ...
}

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...

触ぅ动初心 2024-10-03 16:37:54

在 C# 和 Java 中,可以执行如下操作:

public class MyClass {
    private int x;

    public void DoSomething(int x) {
        int a = this.x;
        int b = x;
    }
}

变量 a 和 b 将具有来自不同 x 的值,因为 1 是类的 x,1 是方法 x。

现在,想象一下如果您无法使用 this 显式引用类的 x.然后你必须执行以下操作:

public class MyClass {
    private int classX;

    public void DoSomething(int x) {
        int a = classX;
        int b = x;
    }
}

这就是你在 JavaScript 中遇到的情况,差不多。至少在你描述的情况下是这样。通过使用 applycall 方法,您可以更改函数执行的上下文,但您永远无法区分具有相同名称但不同作用域的变量。您只需为此使用不同的名称即可。

In C# and Java, one can do something like this:

public class MyClass {
    private int x;

    public void DoSomething(int x) {
        int a = this.x;
        int b = x;
    }
}

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:

public class MyClass {
    private int classX;

    public void DoSomething(int x) {
        int a = classX;
        int b = x;
    }
}

That i the situation you have in JavaScript, pretty much. At least in the situation you're describing. By using the methods apply and call 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.

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