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 之间的区别

规范

SpecificationStatusComment
ECMAScript 1st Edition (ECMA-262)StandardInitial 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

浏览器兼容性

FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support(Yes)(Yes)(Yes)(Yes)(Yes)

FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)