If I understand correctly this code?

Hello
In the process of solving the problem of JS and a couple of hours spent searching for solution, I still found it (the link) and that worked for me :)

This is a problem associated with a partial application (partial application)
Head's a mess, but in principle I understand what I'm doing, but then to explain can be problematic)

/*Function sums any number of arguments passed to it*/
function add(a, b) { 
 var r = 0;
 for(var i in arguments){
 r += arguments[i];
}
 return r; 
}

/*Here I had no understanding. */
function partial(arg) { /*(1)*/
 var param = Array.prototype.slice.call(arguments, 1); /*(2)*/
 return function(){ /*(3)*/
 return arg.apply(this, param.concat(Array.prototype.slice.call(arguments, 0))); /*(4)*/
}
}
var p = partial(add, 2,5); /*(5)*/
console.log(p(1,3,1)); /*(6)*/


Do I understand their actions ?)
  1. The function takes any number of arguments, it is clear.
  2. param is an array of function arguments. It boards all the arguments after the first? That is, the first is ignored?
  3. Returns an anonymous function, it is also clear.
  4. Returns an array of arguments generated from the arrays passed in parameters. And then, by analogy with paragraph 2. I correctly understood? If not then explain)
  5. It's simple: the arguments passed to the function, which are the first 2(in this case) arguments 2 and 5
  6. Passed any number of arguments that will be used in conjunction with already transmitted(i.e. 2 and 5)


If something wrong, correct me)
July 2nd 19 at 14:10
1 answer
July 2nd 19 at 14:12
Solution
All right, analogue .bind
This currying from functional programming. - Rene.DAmore commented on July 2nd 19 at 14:15
Thank you!) - Lavern commented on July 2nd 19 at 14:18

Find more questions by tags JavaScript