从匿名包装返回函数? [英] Return a function from the anonymous wrapper?

查看:75
本文介绍了从匿名包装返回函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图理解代码

for(var i = 0; i < 10; i++) {
    setTimeout((function(e) {
        return function() {
            console.log(e);
        }
    })(i), 1000)
}

从此处 http://bonsaiden.github.com/JavaScript-Garden/# function.closures

我了解这种方法:

for(var i = 0; i < 10; i++) {
    (function(e) {
        setTimeout(function() {
            console.log(e);  
        }, 1000);
    })(i);
}

有人可以通过解释第一个来帮助我吗?

Can anyone please help me by explaining the first one?

我将尝试解释我如何理解第一个,

I will try to explain how I understands the first one,

first i is 0,
setTimeout is called,
self calling function "function(e)" is called with i=0,
Im stuck!! what happens when this function returns a function?

推荐答案

能否请您检查更新的问题,以指明我在哪里感到困惑"

好的,这是 long 的说明.请记住,setTimeout()的第一个参数需要是要在指定的延迟后执行的函数的引用.最简单的情况是仅命名在其他地方定义的函数:

OK, here's the long explanation. Remember that the first parameter to setTimeout() needs to be a reference to the function that you want executed after the specified delay. The simplest case is to just name a function defined elsewhere:

function someFunc() {
   console.log("In someFunc");
}

setTimeout(someFunc, 100);

请注意,当将someFunc作为参数传递给setTimeout时,在someFunc上没有括号,因为需要引用函数本身.与此相反:

Note there are no parentheses on someFunc when passing it as a parameter to setTimeout because a reference to the function itself is required. Contrast with:

setTimeout(someFunc(), 100);   // won't work for someFunc() as defined above

使用括号将其调用 someFunc(),并将其返回值传递给setTimeout.但是我对上面的someFunc()的定义没有明确返回值,因此它隐式地返回undefined-就像说setTimeout(undefined, 100).

With parenthese it calls someFunc() and passes its return value to setTimeout. But my definition of someFunc() above doesn't explictly return a value, so it implicitly returns undefined - which is like saying setTimeout(undefined, 100).

但是如果将someFunc()更改为返回函数而不是返回undefined,则将起作用:

But it would work if changed someFunc() to return a function instead of returning undefined:

function someFunc() {
   return function() {
      console.log("In the function returned from someFunc");
   };
}

现在(最后),我们从您的问题中获取代码:

So now (at last) we come to the code from your question:

setTimeout((function(e) {
    return function() {
        console.log(e);
    }
})(i), 1000)

定义一个匿名函数并立即将其命名为(function(e) {})(i),而不是通过名称引用函数并将其命名为someFunc(i).该匿名函数返回另一个函数,而该返回的函数成为setTimeout()的实际参数.时间到了,将执行返回的函数.因为返回的(内部)函数是在(外部)匿名函数的范围内定义的,所以它可以访问e参数.

Instead of referencing a function by name and calling it as someFunc(i) it defines an anonymous function and calls it immediately as (function(e) {})(i). That anonymous function returns another function and it is that returned function that becomes the actual parameter to setTimeout(). When the time is up it is that returned function that will be executed. Because the (inner) function being returned is defined in the scope of the (outer) anonymous function it has access to the e parameter.

这篇关于从匿名包装返回函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆