arguments的使用
1. 介绍
当函数中形参个数不确定时,可以采用arguments接收。
arguments是一个伪数组,其特点有以下:
- 具有length属性
- 可以按索引方式存储数据
- 不可以使用数组的push、pop方法
2. 使用案例:
function maxValue() {
var max = arguments[0];
for (var i = 0; i < arguments.length; i++) {
if (max < arguments[i]) {
max = arguments[i];
}
}
return max;
}
console.log(maxValue(2, 4, 5, 9));
console.log(maxValue(12, 4, 9))