尝试完全理解 JavaScript 提升

发布于 2024-12-05 20:13:53 字数 1012 浏览 8 评论 0原文

编辑 看来这是我和我对 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);
})();

fooundefined,并且 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

哥,最终变帅啦 2024-12-12 20:13:53

将小提琴中的 wrap 更改为 nowrapp(head)

http:// /jsfiddle.net/rlemon/VjCqF/3/

Change the wrap in the fiddle to no wrap(head)

http://jsfiddle.net/rlemon/VjCqF/3/

笑咖 2024-12-12 20:13:53

您已将 JSFiddle 设置为 onLoad,因此 var a 的作用域为 onLoad 函数。将其更改为无换行,您将得到:

undefined
undefined
Uncaught ReferenceError: baz is not defined

You have JSFiddle set to onLoad, so var a is scoped to the onLoad function. Change it to no wrap and you get:

undefined
undefined
Uncaught ReferenceError: baz is not defined
鹿童谣 2024-12-12 20:13:53

在第二个示例中,提升将代码转换为:

(function bar(){
    var foo;
    console.log(foo);
    foo = 10;  
    console.log(baz);
})();

因此 foo 在第一次调用 log 期间未定义。声明总是被提升到封闭函数的顶部,但赋值永远不会随之提升。

In the second example, hoisting transforms the code into this:

(function bar(){
    var foo;
    console.log(foo);
    foo = 10;  
    console.log(baz);
})();

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.

淑女气质 2024-12-12 20:13:53

object.a === 未定义; // true,但是a in object; // 正确

var object = {a: undefined};

('a' in object); // is true because a is a property of object

object.a === undefined; // true, but a in object; // true

var object = {a: undefined};

('a' in object); // is true because a is a property of object
情深已缘浅 2024-12-12 20:13:53

提升的理解不同,但据我了解,它的作用是

每当您创建变量/函数时,它都会为您设置一个内存空间。它不会将变量/函数放在执行上下文的顶部。

在控制台中输入“this/window”,您会发现您的 var/functions 按词法坐在那里。

例如:

a; //returns undefined 
b(); //returns Hello World   
var a = "Hello";
    function b() {
        console.log('Hello World');
    }

如果在执行期间将其放置在顶部,那么它应该输出“Hello”,而不是“未定义”函数将被执行。变量将不会被执行。
请避免在声明后执行变量的提升:

var a = "Hello";
    function b() {
        console.log('Hello World');
    }
a; //returns Hello

b(); //returns Hello World   

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:

a; //returns undefined 
b(); //returns Hello World   
var a = "Hello";
    function b() {
        console.log('Hello World');
    }

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:

var a = "Hello";
    function b() {
        console.log('Hello World');
    }
a; //returns Hello

b(); //returns Hello World   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文