把《You dont’t know Javascript》上篇看了一些,第一部分作用域和闭包看完了,第二部分除了第4、6章,其他都看完了。
本篇文章想通过几个例子,深入到作用域、变量提升、this的原理。
提升
我们都知道变量会提升,那么这些面试题你能保证都做对吗?
先看一下这些题,由简到难:
1 | // eg1 |
答案:
- 2
- undefined
- undefined
- TypeError: foo is not a function
- ReferenceError: bar is not defined
- TypeError: foo is not a function (ReferenceError: bar is not defined,这个错误不会出现,因为被前一个错误中断了)
- 2
- 3
- b