function sum() { return [].reduce.call(arguments, function(a, b) { return a + b; }); } function mul() { return [].reduce.call(arguments, function(a, b) { return a * b; }); } function applyAll(func) { return func.apply(this, [].slice.call(arguments, 1)); } alert( applyAll(sum, 1, 2, 3) ); // 6 alert( applyAll(mul, 2, 3, 4) ); // 24 alert( applyAll(Math.max, 2, -2, 3) ); // 3 alert( applyAll(Math.min, 2, -2, 3) ); // -2 

help to understand the code ... namely these lines

 return func.apply(this, [].slice.call(arguments, 1)); return [].reduce.call(arguments, function(a, b) { return a * b; }); 
  • Show the code in full, at the end you have it cut off. And what do you want to tell you what these lines mean? - Mr_Epic
  • @Mr_Epic, these are pieces of code from the snippet above. Code enough. - Qwertiy
  • This is the complete code, yes, I do not understand these lines ... - aleksei
  • problem with this function function applyAll (func) {return func.apply (this, [] .slice.call (arguments, 1)); } - aleksei

1 answer 1

 return func.apply(this, [].slice.call(arguments, 1)); 

From arguments first arguments cut off (this is func ), and the tail ( [].slice.call(arguments, 1) ) is passed to apply .

 var a = [func, 1, 2, 4]; a.slice(1); // [1, 2, 4] 

Since arguments is not a real array, you cannot use arguments.slice , since there is no such method. Instead, the Array.prototype.slice array method is applied to them - in a shorter record [].slice - access via an unnecessary instance instead of a direct one.

 return [].reduce.call(arguments, function(a, b) { return a + b; }); 

The usual use of the reduce function. For the same reason as above, calling through the method of an unnecessary array.