arguments 大概是这个样子
arguments = {
1:'a',
2:'b',
3:'c',
length:3,
caller:function (){/*这个函数就是当前函数本身*/} }
arguments 是一个类数组 他不是真正的数组
箭头函数没有arguments
arguments 的基本使用
function foo(n1,n2){
// 获取当前参数
console.log(arguments.length)
// 根据索引获取对应参数
console.log(arguments[0],arguments[3])
// 获取当前函数
console.log(arguments.callee)
}
foo(10,20,30,40,50)
arguments 转 Array
function foo(n1,n2){
var arr = Array.prototype.slice.call(arguments)
console.log(arr)
var arr1 = [].slice.call(arguments)
console.log(arr1)
var arr2 = Array.from(arguments)
console.log(arr2)
var arr3 = [...arguments]
console.log(arr3)
}
foo(10,20,30,40,50)
Array.prototype.slice 实现原理
Array.prototype.myslice = function(start,end){
var arr = this
start = start || 0
end = end || arr.length
var newArr = []
for(i=start;i<end;i++){
newArr.push(arr[i])
}
return newArr
}
var newArr = Array.prototype.slice.call([10,20,30,40,50])
console.log(newArr)
var newArr1 = Array.prototype.slice.call([10,20,30,40,50],1,3)
console.log(newArr1)