admin管理员组文章数量:1391818
Previous posts have talked about how Array.prototype.slice.call(arguments)
work but I don't get why you're using call
instead of apply
when apply is used for array-like objects whereas call
is used on lists of objects separated by mas. Isn't arguments
an array-like object that should used apply
instead of call
?
Previous posts have talked about how Array.prototype.slice.call(arguments)
work but I don't get why you're using call
instead of apply
when apply is used for array-like objects whereas call
is used on lists of objects separated by mas. Isn't arguments
an array-like object that should used apply
instead of call
?
-
3
Even if you used
apply
instead ofcall
,arguments
is thethisArg
, not theargsArray
, so it wouldn't change anything. – user2357112 Commented Sep 28, 2016 at 0:10 - 1 Were you intending to link to this post? how does Array.prototype.slice.call() work? – Jonathan Lonowski Commented Sep 28, 2016 at 0:19
-
2
The 1st argument passed to each method is the same. The difference between them is in the 2nd or further arguments.
.call(thisArg, arg0, arg1, ...)
vs.apply(thisArg, argsList)
. The value given forthisArg
bees the value ofthis
used by the function being invoked (slice
). – Jonathan Lonowski Commented Sep 28, 2016 at 0:21 -
2
Side note:
Array.from()
was added to a recent edition of the language to in part replace this use ofslice
. – Jonathan Lonowski Commented Sep 28, 2016 at 0:27 - 1 Possible duplicate of What is the difference between call and apply? – Felix Kling Commented Sep 28, 2016 at 0:28
2 Answers
Reset to default 4If you'd like to pass arguments to slice in array instead of one by one, then there is a difference. You could do it this way
[1, 2, 3, 4, 5, 6, 7] ---- our example arguments
Array.prototype.slice.call(arguments, 2, 5);
here you pass to slice method 2 and 5 and these are slice arguments to indicate that you want to get items at index 1 to item at index. 4. So most likely it will be 3, 4, 5.
Array.prototype.slice.apply(arguments, [2, 5]);
This does the same but arguments for slice can be passed in an array.
If x
is an array, then these are the same:
// find the "slice" method of an array and call it with "x" as its "this" argument
x.slice();
// find the "slice" method of an array and call it with "x" as its "this" argument
Array.prototype.slice.call(x);
The first argument of func.call
is this this argument, or, the value of this
inside the function. The remaining arguments are the arguments of the function (in this case there are none).
The slice method, called with no arguments, simply creates a new array with the same contents. That's what we want to do when we do Array.prototype.slice.call(arguments)
, however, since arguments
isn't an array and therefore has no slice
method attach to itself, we have to use the second of the two methods to get the slice
method of an array, and then apply that to something that's not an array:
// find the "slice" method of an array and call it with "arguments",
// which is *not* an array, as its "this" argument
Array.prototype.slice.call(arguments);
本文标签: javascriptArrayprototypeslicecall(arguments) vs Arrayprototypesliceapply(arguments)Stack Overflow
版权声明:本文标题:javascript - Array.prototype.slice.call(arguments) vs. Array.prototype.slice.apply(arguments) - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744770598a2624315.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论