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.
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
.
The length property of the
Function
prototype object has a value of 0.
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. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
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 |
完整支持
不支持
Function
Function.arguments
Function.caller
Function.displayName
Function.length
Function.name
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()