Array.prototype.splice – help to understand a lesson

  • A+
Category:Languages

Here is a function from a tutorial:

function add() {     var values = Array.prototype.splice.call(arguments, [1]),         total = 0;      for(var value of values) {         total += value;     }      return total; } 

SOURCE

And the expression Array.prototype.splice.call(arguments, [1]) confuses me.

  1. Why 1?
  2. And why with brackets [1]?

If we pass 1, it represents start position in splice(), so it will skip the first argument we pass to add(), hence it won't add all arguments...

Is this a mistake in the tutorial?


Yes this example is mistaken, if you try the code it doesn't work (it ignores the first parameter) exactly as you said. The code would make sense if that line was:

var values = Array.prototype.slice.call(arguments), 

Or

var values = Array.prototype.splice.call(arguments, 0), 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: