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-expression/ 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/[email protected]/
To search via a non-Google archive, visit here:
http://www.mail-archive.com/[email protected]/
To unsubscribe from this group, send email to
[email protected]