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.
- Why
1
? - 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.
- Why
1
? - 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 to1
withinsplice
. However,apply
would make more sense thancall
(or, of course,call
, but then1
instead of[1]
). And yes, this saves all arguments except the first one invalues
. 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
3 Answers
Reset to default 6Yes 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
版权声明:本文标题:javascript - Array.prototype.splice - help to understand a lesson - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745300380a2652335.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论