js中arguments详解

1.当形参 数量不确定时,可以通过arguments对象获取传参的值。(为一个数组)
function max(a) {
console.log(a);
var max = arguments[0];
console.log(arguments);//传入形参的值,数组形式。
for (val of arguments) {
if (val >= max) {
max = val;
}
}
return max;
}
var maxValue = max(1, 2, 4)
console.log(maxValue); // 输出4

浙公网安备 33010602011771号