length property indicates the number of parameters expected by the function.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

特性属性在 Function.length
可写 no
可枚举 no
可配置 yes

描述

length is a property of a function object, and indicates how many arguments the function expects, i.e. the number of formal parameters. This number excludes the 其余参数 and only includes parameters before the first one with a default value. By contrast, arguments.length is local to a function and provides the number of arguments actually passed to the function.

Data property of the Function constructor

Function constructor is itself a Function object. Its length data property has a value of 1. The property attributes are: Writable: false , Enumerable: false , Configurable: true .

Property of the Function prototype object

The length property of the Function prototype object has a value of 0.

范例

Using function length

console.log(Function.length); /* 1 */
console.log((function()        {}).length); /* 0 */
console.log((function(a)       {}).length); /* 1 */
console.log((function(a, b)    {}).length); /* 2 etc. */
console.log((function(...args) {}).length);
// 0, rest parameter is not counted
console.log((function(a, b = 1, c) {}).length);
// 1, only parameters before the first one with
// a default value is counted
					

规范

规范
ECMAScript (ECMA-262)
The definition of 'Function.length' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
length Chrome 1 Edge 12 Firefox 1 IE 4 Opera 3 Safari 1 WebView Android 1 Chrome Android 18 Firefox Android 4 Opera Android 10.1 Safari iOS 1 Samsung Internet Android 1.0 nodejs Yes
Configurable: true Chrome 43 Edge 12 Firefox 37 IE No Opera 30 Safari No WebView Android 43 Chrome Android 43 Firefox Android 37 Opera Android 30 Safari iOS No Samsung Internet Android 4.0 nodejs Yes

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: