Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, I'm trying to explain it piece by piece:</p> <pre><code>Function.prototype.partial = function() { var fn = this, args = Array.prototype.slice.call(arguments); return function() { var arg = 0; for (var i = 0; i &lt; args.length &amp;&amp; arg &lt; arguments.length; i++) { if (args[i] === undefined) { args[i] = arguments[arg++]; //This line is where the confusion is } } return fn.apply(this, args); }; }; </code></pre> <p>First line:</p> <pre><code>var fn = this, args = Array.prototype.slice.call(arguments); </code></pre> <p>This stores the value of <code>this</code> and the value of <code>arguments</code> in two variables, because both values would get overriden in the following function block:</p> <pre><code>return function() { //inside here arguments will be whatever is passed to this returned function later. }; </code></pre> <p>The for loop:</p> <pre><code>var arg = 0; for (var i = 0; i &lt; args.length &amp;&amp; arg &lt; arguments.length; i++) { } </code></pre> <p>It will basically loop through all arguments that were passed to the <code>partial</code> function and exits earlier if <code>arg &gt;= arguments.length</code>.</p> <pre><code>if (args[i] === undefined) { args[i] = arguments[arg++]; //This line is where the confusion is } </code></pre> <p>So, if an argument of the <code>args</code> array is undefined, we replace it with the next argument in the <code>arguments</code> array. When every arguments are replaced, the original function gets called with the merged arguments array:</p> <pre><code> return fn.apply(this, args); </code></pre> <p>Here is how it would work in practice:</p> <pre><code> function xy(arg1, arg2) { console.log(arg1 + " / " + arg2); } var p1 = xy.partial("foo", undefined); p1("bar") //"foo / bar" var p2 = xy.partial(undefined, "bar"); p2("foo") //"foo / bar" var p3 = xy.partial("foo"); p3("bar") //"foo / undefined" --&gt; because you have to activly pass "undefined" otherwise the arguments array is too short </code></pre> <p>And last but not least, how this code workes in detail with the example <code>p1 = xy.partial("foo", undefined);</code> :</p> <pre><code>//lets call xy.partial("foo", undefined); Function.prototype.partial = function() { //fn = xy //args = ["foo", undefined] var fn = this, args = Array.prototype.slice.call(arguments); //return function that is assigned to p1 //lets call p1("bar") return function() { //arguments = ["bar"] var arg = 0; //for (var i = 0; i &lt; 2 &amp;&amp; arg &lt; 1; i++) for (var i = 0; i &lt; args.length &amp;&amp; arg &lt; arguments.length; i++) { //first iteration: //args[0] === "foo" -&gt; nothing happend //second iteration: //args[1] === undefined -&gt; args[1] = arguments[0] (= "bar"); arg++; if (args[i] === undefined) { args[i] = arguments[arg++]; //This line is where the confusion is } } //at this point: args = ["foo", "bar"]; //now just call the function with the merged array return fn.apply(this, args); }; }; </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