功能:length
Function 实例的 length 数据属性指示函数期望的参数数量。
¥The length data property of a Function instance indicates the number of parameters expected by the function.
Try it
值
描述
¥Description
Function 对象的 length 属性表示函数需要多少个参数,即形式参数的数量:
¥A Function object's length property indicates how many arguments the function expects, i.e. the number of formal parameters:
相比之下,arguments.length 是函数的本地函数,提供实际传递给函数的参数数量。
¥By contrast, arguments.length is local to a function and provides the number of arguments actually passed to the function.
Function 构造函数本身就是一个 Function 对象。其 length 数据属性的值为 1。
¥The Function constructor is itself a Function object. Its length data property has a value of 1.
由于历史原因,Function.prototype 本身就是一个可调用的。Function.prototype 的 length 属性的值为 0。
¥Due to historical reasons, Function.prototype is a callable itself. The length property of Function.prototype has a value of 0.
示例
使用函数长度
¥Using function length
console.log(Function.length); // 1
console.log((() => {}).length); // 0
console.log(((a) => {}).length); // 1
console.log(((a, b) => {}).length); // 2 etc.
console.log(((...args) => {}).length);
// 0, rest parameter is not counted
console.log(((a, b = 1, c) => {}).length);
// 1, only parameters before the first one with
// a default value are counted
console.log((({ a, b }, [c, d]) => {}).length);
// 2, destructuring patterns each count as
// a single parameter
规范
| Specification |
|---|
| ECMAScript Language Specification # sec-function-instances-length |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also