胶州市城乡建设局网站,做网站教程pdf,网站模板下载,网站开发的体会因为arguments本身并不能调用数组方法#xff0c;它是一个另外一种对象类型#xff0c;只不过属性从0开始排#xff0c;依次为0 1 2…最后还有callee和length属性#xff0c;我们也把这样的对象成为类数组。
常见的类数组还有#xff1a; 1.用getElementsByTagName/Class…因为arguments本身并不能调用数组方法它是一个另外一种对象类型只不过属性从0开始排依次为0 1 2…最后还有callee和length属性我们也把这样的对象成为类数组。
常见的类数组还有 1.用getElementsByTagName/ClassName()获得的HTMLCollection 2.用querySelector获得的nodeList 那这就导致很多数组的方法就不能用了必要时需要我们将它们转换为数组有哪些方法呐
1.Array.prototype.slice.call()
function sum(a,b){let args Array.prototype.slice.call(arguments);console.log(args.reduce((sum,cur)sumcur));//args可以调用数组原生的方法
}
sum(1,2);//2.Array.from()
function sum(a,b){let args Array.from(arguments);console.log(args.reduce((sum,cur)sumcur));//args可以调用数组原生的方法
}
sum(1,2);//33.ES6展开运算符
function sum(a,b){let args [...arguments];console.log(args.reduce((sum,cur)sumcur));//args可以调用数组原生的方法
}
sum(1,2);//34.利用concatapply
function sum(a,b){let args Array.prototype.concat.apply([],arguments);//apply方法会把第二个参数展开console.log(args.reduce((sum,cur)sumcur));//args可以调用数组原生的方法
}
sum(1,2);//3