9

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?

8
  • 5
    [1] would just get coerced to 1 within splice. However, apply would make more sense than call (or, of course, call, but then 1 instead of [1]). And yes, this saves all arguments except the first one in values. Honestly, slice would make more sense here… Commented Apr 30, 2018 at 7:26
  • 1
    Thanks, @Xufox - but in either case (slice / splice), that 1 doesn't make sense, right? It should be 0, as I understand Commented Apr 30, 2018 at 7:31
  • Depends on the argument list passed to the add function really.
    – Parama Kar
    Commented Apr 30, 2018 at 7:36
  • @fromZerotoHero I can just guess that it’s just an arbitrary example, even if it really doesn’t make complete sense. Commented Apr 30, 2018 at 7:38
  • 3
    Theyve clearly copied their later example of a calculate method which uses the first argument as the operation and forgotten to correct it.
    – Jamiec
    Commented Apr 30, 2018 at 7:51

3 Answers 3

6

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),
5
  • Did you mean splice or slice?
    – Bergi
    Commented Apr 30, 2018 at 13:59
  • The first example I gave is slice, the second is splice Commented May 28, 2020 at 11:59
  • But only one of them makes sense.
    – Bergi
    Commented May 28, 2020 at 14:06
  • They both work and make sense as written - I don't see any mistake in what I wrote? Commented May 28, 2020 at 16:50
  • 1
    I was referring to "The code would make sense if that line was". You should never mutate the arguments object.
    – Bergi
    Commented May 28, 2020 at 16:57
2

My guess: the example was made by simplyfing code of another function which had one special parameter and used apply instead of call in ES5 syntax.

In the further part of the tutorial, calculating functions are discussed, whose first argument determines the type of calculations to be performed. If you write them in the ES5 syntax, you'd have to delete the first argument. That explains Why 1 - to delete the first argument. Now, why brackets: there are two nearly identical functions in JS: call and apply. See this note about apply:

While the syntax of this function is almost identical to that of call(), the fundamental difference is that call() accepts an argument list, while apply() accepts a single array of arguments.

I think the author of calculation function mistakenly used the syntax for apply and hence the brackets.

1

You are right, calling the Array.prototype.splice with [1] is probably a mistake.

According to MDN docs for splice, this method's first argument is supposed to be a number. Chrome will indeed interpret [1] as 1 and skip the first argument.

You should check if the first argument is supposed to be skipped, otherwise better make your for loop directly over the function arguments.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.