简体   繁体   English

Array.prototype.slice.call(参数)vs. Array.prototype.slice.apply(参数)

[英]Array.prototype.slice.call(arguments) vs. Array.prototype.slice.apply(arguments)

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 commas. 以前的文章已经讨论了Array.prototype.slice.call(arguments)工作方式,但是当将apply用于类似数组的对象而将call用于分离的对象列表时,我不明白为什么要使用call而不是apply用逗号分隔。 Isn't arguments an array-like object that should used apply instead of call ? 是不是arguments数组类对象,应使用apply的,而不是call

If 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. 在这里,您传递给slice方法2和5,这是slice参数,表示您想要将索引1处的项目获取到索引处的项目。 4. So most likely it will be 3, 4, 5. 4.因此最有可能是3、4、5。

Array.prototype.slice.apply(arguments, [2, 5]);

This does the same but arguments for slice can be passed in an array. 这样做相同,但是slice的参数可以在数组中传递。

If x is an array, then these are the same: 如果x是一个数组,则它们是相同的:

// 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. 的第一个参数func .call是这参数,或值this函数内部。 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. slice方法(不带任何参数)仅创建具有相同内容的新数组。 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: 这就是我们执行Array.prototype.slice.call(arguments)时要执行的操作,但是,由于arguments不是数组 ,因此自身没有slice方法,因此必须使用这两种方法中的第二种得到slice 阵列的方法,然后应用的东西,这不是一个数组:

// 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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 arguments vs Array.prototype.slice.call(arguments,0) - arguments vs Array.prototype.slice.call(arguments,0) 调用 Array.prototype.slice.apply(arguments) 时结果不同 - Different result when calling Array.prototype.slice.apply(arguments) Function.prototype.call.bind(Array.prototype.slice)与Array.prototype.slice.call - Function.prototype.call.bind(Array.prototype.slice) vs. Array.prototype.slice.call 鉴于“arguments”不是一个真正的数组,为什么Array.prototype.slice.call(arguments)工作,但是Array.prototype.slice.call(someobject)不起作用? - Given that “arguments” is not a true array, why does Array.prototype.slice.call(arguments) work, but Array.prototype.slice.call(someobject) not work? 为什么将Array.prototype.slice.call与参数一起使用 - Why use Array.prototype.slice.call with arguments Array.prototype.slice.call(arguments)和Array.apply(null,arguments)之间的区别 - Difference between Array.prototype.slice.call(arguments) and Array.apply(null, arguments) 处理Array.prototype.slice.call(参数)的更清洁的方法 - Cleaner way of handling Array.prototype.slice.call(arguments) Array.prototype.slice.call(arguments) 与 […args] 有什么区别? - What is the difference between Array.prototype.slice.call(arguments) vs […args]? [] .slice.call与Array.prototype.slice.call - [].slice.call vs Array.prototype.slice.call Array.prototype.slice.call()的内部工作 - Internal working of Array.prototype.slice.call()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM