A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/length below:

Function:length - JavaScript | MDN

Function:length

Baseline Widely available

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

尝试一下
function func1() {}

function func2(a, b) {}

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

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

一个数字。

可写 否 可枚举 否 可配置 是 描述

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

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

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

示例 使用 function length
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,只计算第一个具有默认值的参数之前的参数
规范 浏览器兼容性 参见

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4