aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/function/length/index.html
blob: 42532bd8ad8d524ff63f435137e533a930d6e401 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
---
title: Function.length
slug: Web/JavaScript/Reference/Global_Objects/Function/length
tags:
  - Function
  - JavaScript
  - Property
translation_of: Web/JavaScript/Reference/Global_Objects/Function/length
---
<div>{{JSRef}}</div>

<p><code><strong>length</strong></code> 属性指明函数的形参个数。</p>

<p>{{EmbedInteractiveExample("pages/js/function-length.html")}}</p>

<div>{{js_property_attributes(0,0,1)}}</div>

<h2 id="Description" name="Description">描述</h2>

<p><code>length</code> 是函数对象的一个属性值,指该函数期望传入的参数数量,即形参的个数。</p>

<p>形参的数量不包括剩余参数个数,仅包括第一个具有默认值之前的参数个数。</p>

<p>与之对比的是,  {{jsxref("Functions_and_function_scope/arguments/length", "arguments.length")}} 是函数被调用时实际传参的个数。</p>

<h3 id="Function_构造器的属性"><code>Function</code> 构造器的属性</h3>

<p>{{jsxref("Function")}} 构造器本身也是个<a href="/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function">Function</a>。他的 <code>length</code> 属性值为 1 。该属性 Writable: <code>false</code>, Enumerable: <code>false</code>, Configurable: <code>true</code>.</p>

<h3 id="Function.prototype_对象的属性"><code>Function</code>.prototype 对象的属性</h3>

<p> {{jsxref("Function.prototype")}}  对象的 length 属性值为 0 。</p>

<h2 id="Examples" name="Examples">示例</h2>

<pre class="brush: js">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</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES1')}}</td>
   <td>{{Spec2('ES1')}}</td>
   <td>Initial definition. Implemented in JavaScript 1.1.</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-15.3.5.1', 'Function.length')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-function-instances-length', 'Function.length')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>The <code>configurable</code> attribute of this property is now <code>true</code>.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-function-instances-length', 'Function.length')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容">浏览器兼容</h2>

<div>{{Compat("javascript.builtins.Function.length")}}</div>

<h2 id="参见">参见</h2>

<ul>
 <li>{{jsxref("Function")}}</li>
</ul>