diff --git a/docs/function.md b/docs/function.md index 355bc4b..82dea95 100644 --- a/docs/function.md +++ b/docs/function.md @@ -179,7 +179,7 @@ f(1, undefined, 2) // [1, 5, 2] ```javascript function foo(x = 5, y = 6){ - console.log(x,y); + console.log(x, y); } foo(undefined, null) @@ -193,12 +193,12 @@ foo(undefined, null) 指定了默认值以后,函数的`length`属性,将返回没有指定默认值的参数个数。也就是说,指定了默认值后,`length`属性将失真。 ```javascript -(function(a){}).length // 1 -(function(a = 5){}).length // 0 -(function(a, b, c = 5){}).length // 2 +(function (a) {}).length // 1 +(function (a = 5) {}).length // 0 +(function (a, b, c = 5) {}).length // 2 ``` -上面代码中,`length`属性的返回值,等于函数的参数个数减去指定了默认值的参数个数。比如,上面最后一个函数,定义了3个参数,其中有一个参数`c`指定了默认值,因此`length`属性等于3减去1,最后得到2。 +上面代码中,`length`属性的返回值,等于函数的参数个数减去指定了默认值的参数个数。比如,上面最后一个函数,定义了3个参数,其中有一个参数`c`指定了默认值,因此`length`属性等于`3`减去`1`,最后得到`2`。 这是因为`length`属性的含义是,该函数预期传入的参数个数。某个参数指定默认值以后,预期传入的参数个数就不包括这个参数了。同理,rest参数也不会计入`length`属性。 @@ -206,6 +206,13 @@ foo(undefined, null) (function(...args) {}).length // 0 ``` +如果设置了默认值的参数不是尾参数,那么`length`属性也不再计入后面的参数了。 + +```javascript +(function (a = 0, b, c) {}).length // 0 +(function (a, b = 1, c) {}).length // 1 +``` + ### 作用域 一个需要注意的地方是,如果参数默认值是一个变量,则该变量所处的作用域,与其他变量的作用域规则是一样的,即先是当前函数的作用域,然后才是全局作用域。 diff --git a/docs/iterator.md b/docs/iterator.md index 5641c39..c95ae4e 100644 --- a/docs/iterator.md +++ b/docs/iterator.md @@ -20,7 +20,7 @@ Iterator的遍历过程是这样的。 每一次调用`next`方法,都会返回数据结构的当前成员的信息。具体来说,就是返回一个包含`value`和`done`两个属性的对象。其中,`value`属性是当前成员的值,`done`属性是一个布尔值,表示遍历是否结束。 -下面是一个模拟next方法返回值的例子。 +下面是一个模拟`next`方法返回值的例子。 ```javascript var it = makeIterator(['a', 'b']); @@ -172,7 +172,6 @@ function Obj(value){ } Obj.prototype[Symbol.iterator] = function(){ - var iterator = { next: next }; @@ -182,7 +181,7 @@ Obj.prototype[Symbol.iterator] = function(){ function next(){ if (current){ var value = current.value; - var done = current === null; + var done = current.next === undefined; current = current.next; return { done: done,