- 将函数的实际参数转换成数组的方法
方法一:var args = Array.prototype.slice.call(arguments);
方法二:var args = [].slice.call(arguments, 0);
方法三:
var args = [];
for (var i = 1; i < arguments.length; i++) {
args.push(arguments[i]);
}
2.Array.prototype.slice.call(arguments)能将具有length属性的对象转成数组,除了IE下的节点集合(因为ie下的dom对象是以com对象的形式实现的,js对象与com对象不能进行转换)
如:
var a={length:2,0:‘first’,1:‘second’};//类数组,有length属性,长度为2,第0个是first,第1个是second
console.log(Array.prototype.slice.call(a,0));// [“first”, “second”],调用数组的slice(0);
var a={length:2,0:‘first’,1:‘second’};
console.log(Array.prototype.slice.call(a,1));//[“second”],调用数组的slice(1);
var a={0:‘first’,1:‘second’};//去掉length属性,返回一个空数组
console.log(Array.prototype.slice.call(a,0));//[]
function test(){
console.log(Array.prototype.slice.call(arguments,0));//[“a”, “b”, “c”],slice(0)
console.log(Array.prototype.slice.call(arguments,1));//[“b”, “c”],slice(1)
}
test(“a”,“b”,“c”);
3.Array.of()和…方法类似。
4.Array。find()找到第一个符合条件的值 参数和 forEach一样。如果没找到返回undefined
Array.findIndex找到的是第一个符合条件的索引 如果没找到返回-1
5.arr.fill(填充的东西,开始位置,结束位置)。
6.arr。includes() 返回ture或false 和 str。includes方法类似。
参数转数组技巧
本文介绍如何将函数的实际参数转换为数组的多种方法,包括使用Array.prototype.slice.call()、Array.of()及扩展运算符等,并探讨了这些方法在不同场景下的应用。
507

被折叠的 条评论
为什么被折叠?



