Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's the benefit of inline function calls?
    primarykey
    data
    text
    <p>I saw this code (explicly it's in jQuery, with modification)</p> <pre><code>(function(window,undefined){ var jQuery=(function(){ var jQuery=something; jQuery.xxx=xxx; //... return jQuery; })(); //... window.jQuery=window.$=jQuery; })(window); </code></pre> <p>While I understand wrapping code in a inline function call can clearly define the variable scope, I don't understand the benefits of</p> <ol> <li>passing <code>window</code> with a parameter instead of using it directly,</li> <li>getting an instance of <code>undefined</code> by a undefined parameter, and also</li> <li>defining <code>jQuery</code> by the return value of another inline function call. Can somebody explain a bit?</li> </ol> <hr> <p><strong>EDIT</strong> write #3 more clearly:</p> <p>What I understand is that the code defines <code>jQuery</code> inside another function then return it.</p> <pre><code>//(function(window,undefined){ var jQuery=(function(){ // Inside this function defines jQuery and return it? var jQuery=function(selector,context){ return new jQuery(selector,context); //simplified }; jQuery.xxx=xxx; //... return jQuery; })(); // This executes the inline function and assign `jQuery` with the return value??? //... })(window); </code></pre> <p>This is more like the following:</p> <pre><code>function define_jQuery(){ // Inside this function defines jQuery and return it? var jQuery=function(selector,context){ return new jQuery(selector,context); //simplified }; jQuery.xxx=xxx; //... return jQuery; } //(function(window,undefined){ var jQuery=define_jQuery(); // This executes the inline function and assign `jQuery` with the return value??? //... })(window); </code></pre> <p>Wouldn't it be more simpler to do:</p> <pre><code>//(function(window,undefined){ var jQuery=function(selector,context){ return new jQuery(selector,context); //simplified }; jQuery.xxx=xxx; //... //... })(window); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload