arguments.length
arguments.length
该arguments.length
属性包含传递给该函数的参数的数量。
语法
arguments.length
描述
arguments.length表示的是实际上向函数传入了多少个参数,这个数字可以比形参数量大,也可以比形参数量小(形参数量的值可以通过Function.length获取到).
示例
使用arguments.length
这个例中,我们定义了一个可以相加任意个数字的函数.
function adder(base /*, n2, ... */) {
base = Number(base
for (var i = 1; i < arguments.length; i++) {
base += Number(arguments[i]
}
return base;
}
注意Function.length
和arguments.length 之间的区别
规范
Specification | Status | Comment |
---|---|---|
ECMAScript 1st Edition (ECMA-262) | Standard | Initial definition. Implemented in JavaScript 1.1 |
ECMAScript 5.1 (ECMA-262)The definition of 'Arguments Object' in that specification. | Standard | |
ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Arguments Exotic Objects' in that specification. | Standard | |
ECMAScript Latest Draft (ECMA-262)The definition of 'Arguments Exotic Objects' in that specification. | Living Standard | |
浏览器兼容性
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|---|
Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |