function a(b) {
var array = Array.prototype.slice.call(b);
console.log(array);
var array_two = ???;
console.log(array_two);
}
a([1, 2, 3], 1, 2);
console.log(array);
按预期提供输出 [1, 2, 3]
。
我想要在这里实现的是获取 [1, 2]
- [ ] 之后的数字,作为数组连接。我认为 b[number]
可以解决问题。 b[0]
- 数组,b[1]
- 数组后的第一个数字,b[2]
- 数组后的第二个数字,但显然它不是那样工作的。你知道有什么解决办法吗?
最佳答案
您可以在a
函数中使用Rest参数
,其中...c
设置为a
的最后一个参数code> 函数,并且可以在a
函数体内作为具有标识符c
的数组进行访问。请参阅What is SpreadElement in ECMAScript documentation? Is it the same as Spread operator at MDN?
function a(b, ...c) {
var array = Array.prototype.slice.call(b);
console.log(array, c);
}
console.log(
a([1, 2, 3], 1, 2)
, a([1,2,3], "a", "b", "c", "d", "e", "f", "g")
);
关于javascript - ([numbers],numbers)作为函数参数,如何从[]之外获取数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39429557/