#1 - function(){ WScript.Echo("foo") }(); #2 - (function(){ WScript.Echo("foo") }()); #3 - (function(){ WScript.Echo("foo") })();
#1 is an error. #2 and #3 both work. If I type "foo" or if I type ("foo") they are semantically the same thing. same as if I type 1 vs (1), etc. The fact that "function(){}()" and "( function(){}() )" are not semantically the same seems a bit backwards logically. Hence I prefer style #3 On Jan 15, 1:30 pm, Bill Heaton <pixelhand...@gmail.com> wrote: > I am curious if anyone can give me insight why the nesting of the function to > be immediately invoked should be completely contained within parenthesis ( // > func () ); versus parenthesis + parenthesis ( // func )(); > > I do not get why the parenthesis (and arguments if needed) would be better > placed just inside the final closing parenthesis ')' > > Below is the typical pattern that I use, following the pattern of jQuery's > source... > > (function($) { > $.fn.pluginName = function(options) { > var defaults = { elem : 'body' }, > opts = $.extend({}, defaults, options); > > return this.each(function() { > // logic and return here > }); > }; > > })(this.jQuery); > > When testing the short template above with jslint.com I get this error... > > Error: > Problem at line 10 character 3: Move the invocation into the parens that > contain the function. > > })(this.jQuery); > > My options using jslint.com were : /*jslint onevar: true, browser: true */ > > +-------------+ > > I did review the "IIFE" article by Cowboy at > :http://benalman.com/news/2010/11/immediately-invoked-function-express...and > noticed that he writes ... > > (function(){ /* code */ })(); // I've been using this one > (function(){ /* code */ }()); // Crockford recommends this one > > +-------------+ > > It seems that this would be better in the opinion of Crockford's (jslint.com) > syntax validation > > (function($) { > $.fn.pluginName = function(options) { > var defaults = { elem : 'body' }, > opts = $.extend({}, defaults, options); > > return this.each(function() { > // logic and return here > }); > }; > > }(this.jQuery)); > > +-------------+ > > So, is the pattern for immediately invoked function better : > > (function( window ) { > var self = window; > return self.location.host; > > }( window )); > > In jQuery source I find this pattern > > (function( window ) { > var self = window; > return self.location.host; > > })( window ); > > +-------------+ > > On another topic, I just quit my day job to go back to full-time contracting > and look forward to the opportunity to discuss Javascript here in this group. > > Thanks in advance for your thoughts on the topic of IIFE and placement of the > parenthesis. > > Best regards, > > -Bill Heaton > @pixelhandler -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com