翻譯不完整。請協助 翻譯此英文文件。
length property表示該 function 預期被傳入的參數數量
Property attributes of Function.length |
|
|---|---|
| Writable | no |
| Enumerable | no |
| Configurable | yes |
描述
length 是 function 物件的一個 property,表示該 function 預期被傳入的參數數量,這個數量並不包含 rest parameter 且只包涵第一個預設參數(Default Parameters)前的參數。相較之下 arguments.length 是 function 內部的物件,會提供真正傳進 function 中的參數數量。
Function 建構子的 data property
Function 建構子本身就是一個 Function 物件。其 length data property 的值為 1。此 property 的 attributes 包含: Writable: false, Enumerable: false, Configurable: true.
Function prototype 物件的 property
Function prototype 物件的 length property 其值為 0。
範例
console.log(Function.length); /* 1 */
console.log((function() {}).length); /* 0 */
console.log((function(a) {}).length); /* 1 */
console.log((function(a, b) {}).length); /* 2 以此類推. */
console.log((function(...args) {}).length); /* 0, rest parameter 不包含在內 */
console.log((function(a, b = 1, c) {}).length); /* 1 */
// 只有在預設參數前的參數會被算到,也就是只有 a 會被視為必須傳入的參數
// 而 c 將被預設為 undefined
規範
| 規範 | 狀態 | 註釋 |
|---|---|---|
| ECMAScript 1st Edition (ECMA-262) | Standard | 最初的定義,在 JavaScript 1.1 中實作。 |
| ECMAScript 5.1 (ECMA-262) The definition of 'Function.length' in that specification. |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Function.length' in that specification. |
Standard | 此 property 的 configurable attribute 現在為 true. |
| ECMAScript 2017 Draft (ECMA-262) The definition of 'Function.length' in that specification. |
Draft |
瀏覽器相容性
| 特點 | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| Configurable: true | ? | 37 (37) | ? | ? | ? |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| Configurable: true | ? | ? | 37.0 (37) | ? | ? | ? |