简体   繁体   English

javascript匿名函数参数传递

[英]javascript anonymous function parameter passing

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 me variable into anonymous the function. 我是javascript的新手,所以尽我所知,实际上将me变量传递me匿名函数。 I was wanting to see if there is a more declarative way to do so? 我想知道是否有一种更具说明性的方式呢? I wanted something along the line of: 我想要的东西是:

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. 我的问题的根源是/我对javascript范围和闭包的理解不清楚。 I found this article to help me understand a little more. 我发现这篇文章可以帮助我理解更多。

You can use ".bind()": 你可以使用“.bind()”:

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

New browsers have "bind()", and the Mozilla docs have a solid implementation you can use to patch older browsers. 新的浏览器具有“bind()”,并且Mozilla文档具有可用于修补旧浏览器的可靠实现。

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 . 不起作用,因为它只是立即调用anon函数,因此立即调用o.progress()并将anon函数(undefined)的返回值赋给handler You need to return an actual function from the outer function: 您需要从外部函数返回实际函数:

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而不是修复它)。


BTW, if the progress function doesn't have any calls to this inside it , just doing handler = this.progress (without the parens) might suffice. 顺便说一句,如果进度功能没有任何电话this里面,只是做handler = this.progress (不含括号)就足够。

The anonymous function has access to me because it is declared inside of the outer function (the toggle function); 匿名函数可以访问me因为它是在外部函数内部声明的( toggle函数); it is closed over by the outer function. 它由外部功能关闭

Your handler function will be called by setInterval , which passes exactly zero arguments. 你的handler函数将由setInterval调用,它正好传递零参数。 This means you can't use parameters in the handler function itself. 这意味着您不能在处理函数本身中使用参数。

I you really want to pass me explicitly, 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 ,你可以编写一个接受参数的函数,并让该函数返回一个没有参数的匿名函数,但是它可以访问creator函数的参数:

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
}

What you have there is a closure. 你有什么关闭。 The function that is created and assigned to handler keeps a reference to the me object. 创建并分配给handler保留对me对象的引用。 This is normal, everyday JavaScript, and that's the way that closures work generally. 这是正常的,日常的JavaScript,这就是闭包一般工作的方式。

Have you tried to return the function like this? 你试过这样的函数吗?

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

Now you can call: 现在你可以打电话:

handler();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM