So foo() is NOT identical to (foo)() where foo is function () {}.
>
> Yes, they are.
>
Dude. Type `function () {} ()` into a browser console and hit enter. That's
my point. It's an error. That's why I say it's not equivalent. You MUST
have the parens.
> No, FunctionExpression (assignments) _do_ _not_ get hoisted.
>
I wasn't referring to the assignment. I was referring to the reference in
the first place.
What actually happens is this
>>
>> function something() {console.log};
>> for () {
>> x = something;
>> }
>>
>
> No, the FunctionExpression assignment _does_ _not_ magically become a
> FunctionDeclaration.
>
>
> console.log( "x" in this );
> for ( var i = 0; i < 1; i++ ) {
> x = function () { console.log(i) }; // <--- NOT hoisted
> }
> false
>
Yes "hoisted". That function is not created there in the loop. It is
created at the top just like
function foo() {
}
And then the reference is what's used in the loop.
By the strictest technical sense you can argue that it's not real hoisting,
but in essence, it is. It behaves as if hoisted. Otherwise the naive
example of
for (i; i < len; i += 1) {
fns.push(function () { console.log(i) });
}
would work. It doesn't work because the function isn't created there. It's
just a reference to the hoisted function. It's hoisted anonymously, but
it's still hoisted.
setTimeout(function(a) { console.log(a) }, 0, 'hello')
>>> hello
>>>
>>
>> Well, duh. Anyone with years of JavaScript experience knows that!
>>
>
> Are you sure? His point was that you were creating the IIFE in a loop
> instead of passing the argument explicitly—which you can.
>
> for ( var i = 0; i < 5; i++ ) {
> setTimeout(function(a) { console.log(a); }, 0, i);
> }
>
Yeah. I know that. And you know that. But, and I quote:
"And when that doesn't work (because it prints *10* ten times) they find
some crazy example online that tells them to do this nonsense:"
I already agree that it's not the good solution. I'm just saying that it's
a common
--
AJ ONeal
(317) 426-6525
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.