尝试完全理解 JavaScript 提升
编辑 看来这是我和我对 jsfiddle 的使用的问题:?
我最近读了几篇关于提升的文章,其中一篇是 尼古拉斯·扎卡斯,另一个是作者:Ben Cherry。
我尝试按照示例进行操作,并自行测试以确保我完全掌握它,但我主要在这个示例中遇到问题,
if (!('a' in window)) {
var a = 1;
}
console.log(a);
而不是记录 undefined
而是记录 1
。如果我正确理解所有内容,a
应该是 undefined
,因为它应该存在于窗口范围内,因为 var 语句被提升到top,所以不应该给它赋值。
但以下内容按预期运行,
(function bar(){
console.log(foo);
var foo = 10;
console.log(baz);
})();
foo
为 undefined
,并且 baz
未定义。我这里有一个 fiddle ,其中包含两个示例。我真的只是想解决这个问题。自这些文章撰写以来,可能发生了一些变化吗?如果有人能对此有所了解,我们将不胜感激。我在测试时使用 Chrome 14。
Edit
Looks like it was an issue on my part and my usage of jsfiddle :?
Ive been reading a couple of articles on hoisting lately, one is by Nicholas Zakas, and the other is by Ben Cherry.
Im trying to follow the examples and just test on my own to make sure I fully grasp it but Im having an issue mainly with this example,
if (!('a' in window)) {
var a = 1;
}
console.log(a);
Instead of logging undefined
its logging 1
. If I am understanding everything correctly, a
should be undefined
, because it should exist in the window scope due to the var statement being hoisted to the top, so it should not be assigned the value.
But the following is acting as expected,
(function bar(){
console.log(foo);
var foo = 10;
console.log(baz);
})();
foo
is undefined
, and baz
is not defined. I have a fiddle here with both examples. Really just trying to wrap my head around this. Has something changed since these articles were written maybe? If anyone can shed some light on this it would be appreciated. Im using Chrome 14 when testing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将小提琴中的
wrap
更改为nowrapp(head)
http:// /jsfiddle.net/rlemon/VjCqF/3/
Change the
wrap
in the fiddle tono wrap(head)
http://jsfiddle.net/rlemon/VjCqF/3/
您已将 JSFiddle 设置为
onLoad
,因此var a
的作用域为 onLoad 函数。将其更改为无换行
,您将得到:You have JSFiddle set to
onLoad
, sovar a
is scoped to the onLoad function. Change it tono wrap
and you get:在第二个示例中,提升将代码转换为:
因此 foo 在第一次调用 log 期间未定义。声明总是被提升到封闭函数的顶部,但赋值永远不会随之提升。
In the second example, hoisting transforms the code into this:
So foo is undefined during the first call to log. The declaration is always hoisted to the top of the enclosing function, but assignment is never hoisted with it.
object.a === 未定义; // true
,但是a in object; // 正确
object.a === undefined; // true
, buta in object; // true
提升的理解不同,但据我了解,它的作用是
每当您创建变量/函数时,它都会为您设置一个内存空间。它不会将变量/函数放在执行上下文的顶部。
在控制台中输入“this/window”,您会发现您的 var/functions 按词法坐在那里。
例如:
如果在执行期间将其放置在顶部,那么它应该输出“Hello”,而不是“未定义”函数将被执行。变量将不会被执行。
请避免在声明后执行变量的提升:
Hoisting is understood differently but to my understanding what it does is
Whenever you create an variable/functions it sets up a memory space for you.It does not put the variable/functions on the top of execution context.
Type 'this/window' in console you'll find your var/functions sitting there lexically.
for example:
If it is placed on top during execution then it should output 'Hello' not 'undefined' functions will get excuted.Variables won't get excuted.
Please avoid hoisting excute the vars after it is being declared: