> 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.
I hate to be that guy but you really need to read the spec. It's not just a
joke that people say, it's actually really quite well-written and helpful.
I know I've spent probably a full day of my life looking at that damn PDF.
Specifically, check out the differences between a FunctionExpression and a
FunctionDeclaration. One is hoisted, one is by definition never hoisted.
Also listen to Rick, I can assure you he knows what he's talking about.
> Yes "hoisted". That function is not created there in the loop. It is
created at the top just like
You are wrong and it's not hoisted. You are creating multiple copies of the
same function.
> It behaves as if hoisted.
No, it doesn't.
var foo = [];
for (var ii = 0; ii < 2; ++ii) {
foo.push(function() {});
}
console.log(foo[0] === foo[1]);
`false`
On Wed, Mar 20, 2013 at 1:10 AM, AJ ONeal <[email protected]> wrote:
> 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.
>
>
>
--
--
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.