当咱们不肯定有多少参数传递的时候,能够使用 arguments 来获取,在 JavaScript 中, arguments 实际上它是当前函数的一个内置对象。数组
全部的函数都内置了一个 arguments 对象,arguments 对象中 存储了传递的全部实参 。浏览器
例如:函数
function numMax(){this
var max=arguments[0]; // arguments 中存储了numMax函数中的全部实参:23,45,96,87 spa
for( var i=1; i<=arguments.length-1;i++){对象
if(arguments[i] > max){索引
max = arguments[i];ip
}io
}console
}
numMax(23,45,96,87);
在调用函数时,浏览器每次都会传递进两个隐含的参数:
1. 函数的上下文对象 this
2. 封装实参的对象 arguments
- arguments 是一个类数组对象,它也能够经过索引来操做数据,也能够获取长度
- 在调用函数时,咱们所传递的实参都会在 arguments 中保存
- arguments . length 能够用来获取实参的长度
- 即便不定义形参,也能够经过 arguments 来使用实参,只不过比较麻烦
arguments[ 0 ] 表示第一个实参
arguments[ 1 ] 表示第二个实参
- arguments 里面有一个属性:callee,属性对应一个函数对象,就是当前正在执行的函数的对象
能够使用 属性 arguments 和函数 fun 作比较,相同返回 true,不然返回 false
console . log( arguments . callee == fun ); // true
例:
function fun(){
console . log( arguments instanceof Array ); // 判断 arguments 是不是数组
console . log( Array . isArray( arguments ) ); // 判断 arguments 是不是数组
console . log( arguments . length ); // fun 函数实参的长度
}
fun( );