简体   繁体   English

变量:局部范围,全局范围还是JavaScript引擎?

[英]Variable: local scope, global scope or is it the JavaScript engine?

Here is something interesting I found while learning scope in javascript. 这是我在javascript中学习范围时发现的有趣内容。

Code

var foo = "This is a global variable.";

var bar = function() {
    alert(foo);
    foo = "Value has been modified";
}

bar();
alert(foo);

This gives the normal response you think you would get, but if I change this one line: 这给出了您认为可以获得的正常响应,但如果我更改了这一行:

from: 从:

foo = "Value has been modified";

to: 至:

var foo = "Value has been modified";

I get a value of undefined for foo, why is this? 我为foo得到了一个未定义的值,为什么会这样? Since the function is global scope how come it doesn't accept the var keyword in front of it? 既然函数是全局范围的,那它怎么不接受前面的var关键字呢?

Edit 编辑

Now I understand basically what is happening the var foo in the function bar will gain most importance because of the var keyword and get hoisted up, but it will be hoisted up without the value it has been assigned. 现在我基本了解发生了什么,函数栏中的var foo因var关键字而获得最大的重要性并被提升,但它将在没有分配值的情况下被提升。

In a var statement, there are two parts to it - the actual declaration: var语句中,它有两个部分 - 实际声明:

var foo //;

... and the assignment, which is optional: ......和分配,这是可选的:

= 1234567890;

If there is no assignment done to it, the variable (if not already defined) defaults to undefined . 如果没有完成赋值,则变量(如果尚未定义)默认为undefined


The variable declaration part is moved to the top of the current scope (the start of the function), but not the actual assignment (so it's equivalent to the following): 变量声明部分移动到当前作用域的顶部(函数的开头),但不是实际的赋值 (因此它等效于以下内容):

var foo = "This is a global variable.";

var bar = function() {
    var foo; // undefined
    alert(foo); // yes, it's undefined
    foo = "Value has been modified"; // modify local, not global
}

bar();
alert(foo); // the global one

Functions create their own scope - for example take this: 函数创建自己的范围 - 例如:

var test = function ()
{   var bar = 1;
    console.log(bar); // 1
};
test();
console.log(bar); // ReferenceError: bar is not defined

By using var , you're telling the engine to use a local variable named foo, shadowing the global one. 通过使用var ,您告诉引擎使用名为foo的局部变量,遮蔽全局变量。

The reason you get undefined on the alert , is that using var affects the whole scope, not just from that point onwards. 您在alert上未定义的原因是使用var会影响整个范围,而不仅仅是从那时起。 You could've written: 你可以写:

var foo;
alert(foo);
foo = "Value has been modified";

The JavaScript engine will parse your code and move var declarations to the top of its scope, but your string assignement to it will stay where it was. JavaScript引擎将解析您的代码并将var声明移动到其作用域的顶部,但是您对它的字符串分配将保持原样。 After the parse, here is how your code is interpreted: 在解析之后,以下是您的代码的解释方式:

var foo = "This is a global variable.";    

var bar = function() {
    var foo;
    alert(foo);
    foo = "Value has been modified";
}    

bar();
alert(foo);

As it creates a local variable without any value yet at the top of your function, your alert will show undefined . 由于它在您的函数顶部创建了一个没有任何值的局部变量,因此您的警报将显示为undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM