Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Why does this need the immediately invoked function called 'resolver'? Immediately invoked functions are usually about establishing a clean namespace, aren't they? What's this one being used for here?

Also (possibly related), what does 'arguments' in "Array.prototype.slice.call( arguments )" resolve to, and when? I would have thought it would be evaluated when 'resolver' in invoked, and would therefore evaluate to an empty array. If so, isn't there a simpler way to make an empty array?



Both good questions.

The immediately invoked function is really just a slightly less verbose way to do this:

    function resolver() {

    }

    return resolver();
The reason that I gave it a name instead of using an anonymous function (as is common with immediately invoked function expressions) is because I need to be able to call it from within itself. This function is all about creating a closure where we can store all of the arguments we've received so far.

As for your second point: The first time that `resolver` is called, arguments is in fact empty. But if you read a bit further, you'll see that `resolver` calls itself recursively (indirectly, via the anonymous function it returns), and does in fact pass arguments. So you can't just start off with an empty array each time. You need to make sure you're taking into account any arguments that were passed in. Does that make sense?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: