Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The <code>()</code> that wrap the function turns the anonymous function declaration into a function expression that can then be immediately invoked with the <code>()</code> that follows the expression.</p> <p>In this case, the outer <code>()</code> really isn't necessary since the <code>var foo =</code> would turn it into an expression. Also, the value of <code>foo</code> will be <code>undefined</code> since the function invocation doesn't return anything.</p> <p>It can be used for creating a new variable scope, since a function is the only way to accomplish that in javascript. (Javascript doesn't have block scope.)</p> <p>So the <code>someVar</code> variable is not accessible to the outer scope. There may be times when it is desirable to make it accessible in a controlled manner. To do this, you can pass a function out of that scope which references <code>someVar</code>. Then after the function invocation exits, its execution context will remain intact, and <code>someVar</code> will be available in whatever manner the function you passed out provides. </p> <p>This is called creating a <code>closure</code>.</p> <p>Let's say you passed a value into the invocation, and assigned it to <code>someVar</code>. You could then <code>return</code> a function out of the invocation to the <code>foo</code> variable. If that function you return references <code>someVar</code>, then you could use that function to get its value.</p> <pre><code>var foo = (function ( str ) { var someVar = str; /* function someFunc() { return true; } */ return function() { alert( someVar ); }; })( 'somevalue' ); foo(); // alerts 'somevalue' </code></pre> <p>As you can see, the function now referenced by <code>foo</code> can still access <code>someVar</code>.</p> <p>Let's say you changed it so that the function returned to <code>foo</code> can accept an argument, which will update the value of <code>myVar</code>.</p> <pre><code>var foo = (function ( str ) { var someVar = str; /* function someFunc() { return true; } */ return function( n ) { if( n ) { someVar = n; } else { alert( someVar ); } }; })( 'somevalue' ); foo(); // alerts 'somevalue' foo( 'newvalue' ); // give it a new value foo(); // alerts 'newvalue' </code></pre> <p>Now you can see, that the function in <code>foo</code> really does access that variable, as it is able to change its value, and reference the new value that it previously set.</p>
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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