They both do the same thing to create an object in the global namespace during code execution.
One of them is no more "hard-coded" than the other, since none of them performs any prototype functions in which you could create clones of an object with a new keyword. In my opinion, this is a matter of preference.
For example, jquery does something similar to the last:
(function( window, undefined ) { // Use the correct document accordingly with window argument (sandbox) var document = window.document; var jQuery = (function() { // Define a local copy of jQuery var jQuery = function( selector, context ) { // The jQuery object is actually just the init constructor 'enhanced' return new jQuery.fn.init( selector, context, rootjQuery ); }, // Map over jQuery in case of overwrite _jQuery = window.jQuery, // Map over the $ in case of overwrite _$ = window.$, ...
But the prototype JS library does the first:
var Prototype = { Version: '1.6.1', Browser: (function(){ var ua = navigator.userAgent; var isOpera = Object.prototype.toString.call(window.opera) == '[object Opera]'; return { IE: !!window.attachEvent && !isOpera, Opera: isOpera, WebKit: ua.indexOf('AppleWebKit/') > -1, Gecko: ua.indexOf('Gecko') > -1 && ua.indexOf('KHTML') === -1, MobileSafari: /Apple.*Mobile.*Safari/.test(ua) } })(), ...
I don’t know a single reason why one is better than the other, or that they perform their task differently (to create an application object in the window namespace).
Jared farrish
source share