edit function/length

This commit is contained in:
ruanyf
2015-11-13 13:22:20 +08:00
parent 0fc76fec74
commit 8afab8ca89
2 changed files with 11 additions and 3 deletions
+7 -1
View File
@@ -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
```
### 作用域