Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
alsotang committed Jan 13, 2016
1 parent a5ed512 commit 2f61700
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lesson11/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ for (var i = 0; i < 5; i++) {
}
```

上面这个代码块会打印五个 `5` 出来,而我们预想的结果是打印 1 2 3 4 5
上面这个代码块会打印五个 `5` 出来,而我们预想的结果是打印 0 1 2 3 4。

之所以会这样,是因为 setTimeout 中的 i 是对外层 i 的引用。当 setTimeout 的代码被解释的时候,运行时只是记录了 i 的引用,而不是值。而当 setTimeout 被触发时,五个 setTimeout 中的 i 同时被取值,由于它们都指向了外层的同一个 i,而那个 i 的值在迭代完成时为 5,所以打印了五次 `5`

Expand Down

0 comments on commit 2f61700

Please sign in to comment.