Javascript javascript匿名函数参数传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6188861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:38:41  来源:igfitidea点击:

javascript anonymous function parameter passing

javascript

提问by Jose

I have some javascript code (within an object) :

我有一些 javascript 代码(在一个对象内):

toggle: function() {
    var me = this;
    var handler = function() { me.progress() };
    me.intervalId = setInterval(handler, me.intervalTime);
    //...More code
}

I'm kind of new to javascript, so doing the above as far as I can tell actually passes the mevariable into anonymous the function. I was wanting to see if there is a more declarative way to do so? I wanted something along the line of:

我对 javascript 有点陌生,所以据我所知,执行上述操作实际上是将me变量传递给匿名函数。我想看看是否有一种更具声明性的方式来做到这一点?我想要一些类似的东西:

var handler = (function(o) { o.progress();})(this));

but that doesn't seem to be working... Am I missing something? Is this a case where "this is the way the language works so just declare a local variable and deal with it"?

但这似乎不起作用......我错过了什么吗?这是“这是语言的工作方式,所以只需声明一个局部变量并处理它”的情况吗?

UPDATE:

更新:

The source to my problem was/is my unclear understanding of scope and closures in javascript. I found this articleto help me understand a little more.

我的问题的根源是/是我对 javascript 中的范围和闭包的理解不清楚。我发现这篇文章可以帮助我了解更多。

回答by Pointy

You can use ".bind()":

您可以使用“.bind()”:

var handler = function() { this.progress(); }.bind(this);

New browsers have "bind()", and the Mozilla docshave a solid implementation you can use to patch older browsers.

新浏览器有“bind()”,Mozilla 文档有一个可靠的实现,你可以用它来修补旧浏览器。

回答by hugomg

The reason

原因

var handler = (function(o) { o.progress();})(this));

doesn't work because it just immediately calls the anon function, therefore immediately calling o.progress()and assigns the return value of the anon function (undefined) to handler. You need to return an actual function from the outer function:

不起作用,因为它只是立即调用 anon 函数,因此立即调用o.progress()并将 anon 函数(未定义)的返回值分配给handler. 您需要从外部函数返回一个实际函数:

handler = (function(me){
    return function(){
        return me.progress();
    }
}(this));

On the flip side this is equivalent and just as bad looking as bad looking as the variable assignment (but can still be useful, particularly if this needs to be done in a loop, with the changing i rather than the fixed this).

另一方面,这是等效的,并且看起来和变量赋值一样糟糕(但仍然有用,特别是如果这需要在循环中完成,改变 i 而不是固定 this)。



BTW, if the progress function doesn't have any calls to thisinside it , just doing handler = this.progress(without the parens) might suffice.

顺便说一句,如果进度函数this内部没有任何调用,只需执行handler = this.progress(不带括号)就足够了。

回答by Martijn

The anonymous function has access to mebecause it is declared inside of the outer function (the togglefunction); it is closed overby the outer function.

匿名函数可以访问,me因为它是在外部函数(toggle函数)内部声明的;它被外部函数关闭

Your handlerfunction will be called by setInterval, which passes exactly zero arguments. This means you can't use parameters in the handler function itself.

您的handler函数将由 调用setInterval,它传递恰好零个参数。这意味着您不能在处理程序函数本身中使用参数。

I you reallywant to pass meexplicitly, you could write a function accepting an parameter, and have that function return an anonymous function without parameters, but which could access the creator function's parameter:

真的me显式传递,你可以编写一个接受参数的函数,并让该函数返回一个没有参数的匿名函数,但它可以访问创建者函数的参数:

toggle: function() {
    var me = this;
    var handler = (function (o) { return function() { o.progress() }; })(me);
    me.intervalId = setInterval(handler, me.intervalTime);
    //...More code
}

But this basically adds a layer of redirection without really making it more legible. Unless you pull that creating function outside:

但这基本上增加了一层重定向,而没有真正使它更清晰。除非你把那个创建函数拉到外面:

function createProgressHandler(o) {
    return function() {
        o.progress();
    };
}

// ...

toggle: function() {
    var me = this;
    var handler = createProgressHandler(me);
    me.intervalId = setInterval(handler, me.intervalTime);
    //...More code
}

回答by Adam Crossland

What you have there is a closure. The function that is created and assigned to handlerkeeps a reference to the meobject. This is normal, everyday JavaScript, and that's the way that closures work generally.

你所拥有的是一个闭包。创建并分配给的函数handler保持对me对象的引用。这是正常的、日常的 JavaScript,也是闭包通常的工作方式。

回答by Thomas

Have you tried to return the function like this?

您是否尝试过像这样返回函数?

var handler = function(o){
   return function(){
      o.progress();
   }
}(me);

Now you can call:

现在你可以调用:

handler();