Function:length

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Function 实例的 length 数据属性表示函数期望的参数数量。

尝试一下

function func1() {}

function func2(a, b) {}

console.log(func1.length);
// Expected output: 0

console.log(func2.length);
// Expected output: 2

一个数字。

Function:length 的属性特性
可写
可枚举
可配置

描述

一个 Function 对象的 length 属性表示函数期望的参数个数,即形参的个数。这个数字不包括剩余参数,只包括在第一个具有默认值的参数之前的参数。相比之下,arguments.length 是局限于函数内部的,它提供了实际传递给函数的参数个数。

Function 构造函数本身就是一个 Function 对象。它的 length 数据属性的值为 1

由于历史原因,Function.prototype 本身是可调用的。Function.prototypelength 属性的值为 0

示例

使用 function length

js
console.log(Function.length); // 1

console.log((() => {}).length); // 0
console.log(((a) => {}).length); // 1
console.log(((a, b) => {}).length); // 2,依此类推

console.log(((...args) => {}).length);
// 0,剩余参数不计算在内

console.log(((a, b = 1, c) => {}).length);
// 1,只计算第一个具有默认值的参数之前的参数

规范

Specification
ECMAScript® 2025 Language Specification
# sec-function-instances-length

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
length
Configurable: true

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见