Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>function (a, b) { var c = a('meta[name="csrf-token"]').attr("content"); }(jQuery)// the function call is made here </code></pre> <p>The first argument provided is <code>jQuery</code>, the big <code>jQuery</code> object, equivalent to the <code>$</code>. In your call, <code>a = jQuery</code> and <code>b = undefined</code> by default, as it was never provided.</p> <pre><code>(function(a, b) { console.log(a); // 5 console.log(b); // 3 })(5, 3); </code></pre> <p>As @dystroy points out, this is a shorter code trick, but it's generally not used to shorten <code>undefined</code>, which can easily be obtained through any param omission. JavaScript is often minified, but minifiers cannot minify default keywords such as <code>document</code> or <code>window</code>. Performance is increased by diminishing the file size.</p> <p>A far more common scenario is:</p> <pre><code>!function(w, d){ w.onload = function() { var x = d.getElementById("whatever"); }; }(window, document); </code></pre> <p>All the above should be IIFEs, or Immediately invoked. Use parentheses or any mathematical operator to force evaluation as an expression.</p> <p><strong>Update</strong></p> <p>Passing parameters to a function.</p> <pre><code>(function(a, b) { // here I am defining an anonymous function. It has no name console.log(a); // It takes two params, a and b. console.log(b); })(5, 3); // Because of the () parentheses around it: (function(){}) // the function is evaluated as an expression. // because of the second group of params (5, 3) the function is called. </code></pre> <p>Imagine you are doing this.</p> <pre><code>function do(a, b) { // bla bla }; do(5, 3); </code></pre> <p>Glue together the function definition and the function call and you get:</p> <pre><code>(function(a, b) {})(5, 3); </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.
    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.
 

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