mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2024-04-21 12:32:22 +00:00
edit function/length
This commit is contained in:
+7
-1
@@ -204,7 +204,13 @@ foo(undefined, null)
|
||||
(function(a, b, c = 5){}).length // 2
|
||||
```
|
||||
|
||||
上面代码中,`length`属性的返回值,等于函数的参数个数减去指定了默认值的参数个数。
|
||||
上面代码中,`length`属性的返回值,等于函数的参数个数减去指定了默认值的参数个数。比如,上面最后一个函数,定义了3个参数,其中有一个参数`c`指定了默认值,因此`length`属性等于3减去1,最后得到2。
|
||||
|
||||
这是因为`length`属性的含义是,该函数预期传入的参数个数。某个参数指定默认值以后,预期传入的参数个数就不包括这个参数了。同理,rest参数也不会计入`length`属性。
|
||||
|
||||
```javascript
|
||||
(function(...args) {}).length // 0
|
||||
```
|
||||
|
||||
### 作用域
|
||||
|
||||
|
||||
Reference in New Issue
Block a user