Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Partial function helps you to pre fill the function arguments that is always needed for the function execution. This helps is reducing the overheads in many ways. </p> <p>The example above helps in filling undefined arguments later.</p> <p>The <em>arg</em> is a private variable to keep track of the undefined arguments and fill the undefined arguments in sequence. </p> <pre><code>args[i] = arguments[arg++]; </code></pre> <p>The above line filling the undefined arguments those were left undefined when using partial.</p> <p>Example:-</p> <p>function a(b,c){ console.log(b,c); }</p> <p>//pre filling the second argument with numeric value 1. But first argument is still undefined. This will return a new function.</p> <pre><code>var g = a.partial(undefined, 1); </code></pre> <p>//Now when you actually invoke the function and pass it the parameters this will fill the arguments left undefined previously i.e. "b" . In this case the <em>var arg</em> will fill the previous <em>args</em> array position those were undefined i.e. args[0] was undefined. So <em>var arg</em> will read the passed arguments in sequence and fill the previously undefined arguments for you.</p> <pre><code>g("Hi I was undefined before"); </code></pre> <p>If don't want to <em>args</em> gets permanently change then update the <em>partial</em> implementation as below.</p> <pre><code>Function.prototype.partial = function() { var fn = this, args = Array.prototype.slice.call(arguments); return function() { var arg = 0, g;//g will hold the clone of args g = args.slice(0);//clone the args array and use that instead of original args. for (var i = 0; i &lt; g.length &amp;&amp; arg &lt; arguments.length; i++) { if (g[i] === undefined) { g[i] = arguments[arg++]; } } return fn.apply(this, g); }; }; function fn(a, b){ } var fn = f.partial(undefined, 3), x = fn(2), y= fn(4);//Now this will work </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. This table or related slice is empty.
    1. 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