On Tue, Mar 19, 2013 at 9:41 PM, AJ ONeal <coola...@gmail.com> wrote:
> > This doesn't do too much to demystify anything, imo. >> >> > Now how that works... I have no idea, but I do believe that it's more >> aesthetic and so that's how I write my code. I won't argue if you feel >> differently. >> > > I suppose I should have been more specific: > > This is an error foo(): > > function () {} () > > This is not (foo)() > > (function () {})() > > This is not (foo()) > > (function () {}()) > > So foo() is NOT identical to (foo)() where foo is function () {}. > Yes, they are. > var foo = function() { return 1; } > foo() 1 > (foo)() 1 > foo === (foo) true The parens around the function object reference are the grouping operator. > > > >> >> It works because (foo)() is identical to foo(). >> > > >> > The only reason that that even works at all is because there's magic >> going on behind the scenes that hoists the anonymous function up to the top >> (just like with variables) and then calls that function reference. >> >> What am I reading? Function expression are not hoisted. >> > > var foo = function () {}; > > foo gets hoisted > > No, FunctionExpression (assignments) _do_ _not_ get hoisted. > foo(); > var foo = function() {}; TypeError: undefined is not a function > > > function foo() {}; > > foo gets hoisted > Yes, because it's a Function Declaration > > > > for () { > x = function () { console.log(i) }; > } > > 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 console.log( "y" in this ); for ( var i = 0; i < 1; i++ ) { function y() { console.log(i) }; // <--- hoisted } true > I probably could word that better since that's not technically > "hoisting", but in essence it's still hoisting. The creation of the > function is pulled out of the loop in the same way `var x` would get pulled > out and only the reference remains. > > Also setTimeout() takes optional parameters to pass to the callback: >> >> 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); } 0 1 2 3 4 > However, years of wretched online tutorials do not. I agree that it's a > good solution (and I should probably show that as an alternate way to do > it. However, a common solution is to through in the IIFE. > > >> Also there's Function.prototype.bind() >> > > Also something I might include when I go back and edit. > Seems like there are a few things you need to edit. Rick -- -- 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 nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscr...@googlegroups.com 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 nodejs+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.