title: JavaScript's bind() with currying. How does these code work?
body: <p>These code was posted on CodeReview two days ago:</p>
  
  <pre><code>function curry(f, self) {
    return function () {
      if (arguments.length == f.length) {
        return f.apply(self, arguments);
      }
      arguments = Array.prototype.slice.call(arguments);
  
      return curry(f.bind.apply(f, [self].concat(arguments)));
    }
  }
  
  function f(a, b, c, d) {
    return this + a + b + c + d;
  }
  
  document.write("f(1, 2, 3, 4) = ", curry(f, 0)(1, 2, 3, 4), "&lt;br&gt;");
  document.write("f(1, 2, 3)(4) = ", curry(f, 0)(1, 2, 3)(4), "&lt;br&gt;");
  document.write("f(1)(2, 3, 4) = ", curry(f, 0)(1)(2, 3, 4), "&lt;br&gt;");
  document.write("f(1)(2)(3)(4) = ", curry(f, 0)(1)(2)(3)(4), "&lt;br&gt;");
  </code></pre>
  
  <p>What I'm not able to understand is: </p>
  
  <p>There is made a new copy of f by using bind(). The parameter already provided are assigned to the copy but what is with the variable "self"?</p>
  
  <p>I've tried to "sketch" what I mean:</p>
  
  <pre><code>// Second parenthesis (marked with =&gt;): There are three of four  
  // expected parameter provided: 
  document.write("f(1, 2, 3)(4) = ", curry(f, 0) =&gt; (1, 2, 3) &lt;= (4), "&lt;br&gt;");
  
  // Makes an array-literal with "self" (== 0) as only element in it.
  // Then adds the parameter already provided to these array by
  // using concat(). =&gt; Results in an array [ 0, 1, 2, 3 ].
  // Then makes a new copy of f with these values bind to it as parameter. 
  // These new instance of the function is then passed to the curry-function.
  return curry(f.bind.apply(f, [self].concat(arguments)));
  </code></pre>
  
  <p>The copy of f should have it's four parameter. It should be executed and resulting "return 0 + 0 + 1 + 2 + 3;" and return 6.</p>
  
  <p>Why isn't that the case?</p>
  
  <p>Perhaps someone can answer that. I would appreciate it. </p>