admin管理员组

文章数量:1419252

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?

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?

Share Improve this question edited Apr 30, 2018 at 7:32 Martin Reiche 1,6721 gold badge16 silver badges27 bronze badges asked Apr 30, 2018 at 7:20 Julius DzidzevičiusJulius Dzidzevičius 11k11 gold badges41 silver badges84 bronze badges 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… – Sebastian Simon 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 – Julius Dzidzevičius 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 plete sense. – Sebastian Simon 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
 |  Show 3 more ments

3 Answers 3

Reset to default 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),

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.

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.

本文标签: javascriptArrayprototypesplicehelp to understand a lessonStack Overflow