Javascript:成员变量访问闭包

发布于 2024-11-06 19:11:59 字数 693 浏览 0 评论 0原文

我想知道如何处理 JavaScript 中闭包中的成员变量。以下代码警报“6”。

function testy()
{
    function blah()
    {
        this.a = 5;
        this.func = function()
        {
            this.a = 6;
            alert(this.a);
        }
    }

    var x = new blah;
    x.func();
}

但此代码会发出警报 5.

function testy()
{
    function execute(func)
    {
        func();
    }

    function blah()
    {
        this.a = 5;
        this.func = function()
        {
            execute(function()
            {
                this.a = 6;
            });

            alert(this.a);
        }
    }

    var x = new blah;
    x.func();
}

如何传递仍访问封闭对象的成员变量的闭包?

I'm wondering how to deal with member variables in closures in JavaScript. The following code alerts "6".

function testy()
{
    function blah()
    {
        this.a = 5;
        this.func = function()
        {
            this.a = 6;
            alert(this.a);
        }
    }

    var x = new blah;
    x.func();
}

but this code alerts 5.

function testy()
{
    function execute(func)
    {
        func();
    }

    function blah()
    {
        this.a = 5;
        this.func = function()
        {
            execute(function()
            {
                this.a = 6;
            });

            alert(this.a);
        }
    }

    var x = new blah;
    x.func();
}

How do I pass a closure which still accesses the member variables of the enclosing object?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

情泪▽动烟 2024-11-13 19:11:59
execute(function()
{
   this.a = 6;
});

function execute(func)
{
    func();
}

您将函数调用为 func(); 并且默认情况下不指定上下文 this 将解析为浏览器中的全局上下文,即 window .. 您可以在此处使用三个选项。

使this本地

var that = this;
execute(function()
{
   that.a = 6;
});

现在that指向正确的this

this 作用域绑定到函数

execute((function()
{
   this.a = 6;
}).bind(this));

这会将正确/预期的 this 作用域绑定到您的函数。请注意 Function.prototype.bind 是 ES5,会破坏旧版浏览器。 _.bind 是一个合理的跨浏览器替代方案。

编辑执行

function execute(f, context) {
    f.call(context);
}

execute(function() {
    this.a = 6;
}, this);

将上下文作为额外参数传递以执行。然后execute会调用 Function.prototype.call< /a> 确保使用所需的上下文调用该函数

execute(function()
{
   this.a = 6;
});

function execute(func)
{
    func();
}

Your calling the function as func(); and by default without specifying a context this will resolve to the global context which is window in the browser.. There are three options you can use here.

make this local

var that = this;
execute(function()
{
   that.a = 6;
});

Now that points to the correct this.

bind this scope to the function

execute((function()
{
   this.a = 6;
}).bind(this));

This will bind the correct / expected this scope to your function. Note that Function.prototype.bind is ES5 and will break older browsers. _.bind is a reasonable cross browser alternative.

edit execute

function execute(f, context) {
    f.call(context);
}

execute(function() {
    this.a = 6;
}, this);

Your passing the context as an extra parameter to execute. Then execute will call Function.prototype.call to make sure that the function is called with the desired context

╰◇生如夏花灿烂 2024-11-13 19:11:59

试试这个:

function blah()
{
    this.a = 5;
    this.func = function()
    {
        var self = this;
        execute(function()
        {
            self.a = 6;
        });
        alert(this.a);
    }
}

Try this:

function blah()
{
    this.a = 5;
    this.func = function()
    {
        var self = this;
        execute(function()
        {
            self.a = 6;
        });
        alert(this.a);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文