function foo(num1, num2) {
// 1.自己遍历
// var newArr = []
// for (var i = 0; i < arguments.length; i++) {
// newArr.push(arguments[i] * 10)
// }
// console.log(newArr)
// 2.arguments转成array类型
// 2.1.自己遍历arguments中所有的元素
// 2.2.Array.prototype.slice将arguments转成array
var newArr2 = Array.prototype.slice.call(arguments)
console.log(newArr2)
var newArr3 = [].slice.call(arguments)//效果是一样的 []调用call隐式绑定了
console.log(newArr3)
// 2.3.ES6的语法
var newArr4 = Array.from(arguments)
console.log(newArr4)
var newArr5 = [...arguments]
console.log(newArr5)
}
foo(10, 20, 30, 40, 50)
js函数中的类数组参数arguments转为数组
于 2022-03-28 15:54:39 首次发布