diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/function/length/index.html')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/function/length/index.html | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/function/length/index.html b/files/zh-cn/web/javascript/reference/global_objects/function/length/index.html new file mode 100644 index 0000000000..9804d07a75 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/function/length/index.html @@ -0,0 +1,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> |