A confusion about JavaScript global variables
ringa_lee
ringa_lee 2017-05-18 10:56:05
0
5
602

The following code:

console.log(b);
b = 2;
//"ReferenceError: b is not defined

I thought it would be undefined when printed.
According to my understanding, b = 2 does not use var, so it declares a global variable. Since the variable is declared, there will be variable declaration promotion. Why is there ReferenceError: b is not defined?

And try adding var:

console.log(b);
var b = 2;
//undefined

So it seems that using var and omitting var are not just the difference between local and global declarations?
As a newbie, please give me some advice, thank you!

ringa_lee
ringa_lee

ringa_lee

reply all(5)
刘奇
// ①
console.log(b); // 代码执行到这行的时候,b未定义,在当前的作用域链上找不到b,所以 b is not defined
b = 2; 

// ②
console.log(b);
var b = 2;
// 由于变量声明提升,相当于
var b;
console.log(b); // 代码执行到这行的时候,b已经声明了,只是没有赋值而已,所以 undefined
b = 2; 

There is always a sequence for code execution. . .

Additional point:
directlyb = 2这种创建全局变量的方式,其实质是变为全局对象上的一个属性,即window.b = 2, so there is no promotion or non-promotion; variables declared using var have declaration promotion.

刘奇

Ask and answer your own questions and add one more thing:

I found this sentence on mdn:

Declared variables are created before any code is executed, while undeclared variables are only created when assignment operations are performed.

This sentence explains my confusion about why the global variable b is not promoted like a. a is a declared variable, while b is an undeclared variable that is only defined during execution, so is not promoted before execution.

Original text: https://developer.mozilla.org...

某草草

Declaration will be made in advance

console.log(a);
var a = 1;

will be interpreted by the compiler as

var a;
console.log(a);
a = 1;

And undeclared/undefined has no chance of being promoted, so an error will be reported as soon as it is used

伊谢尔伦

For these scope issues, you can read "Javascript You Don't Know" (Volume 1), it's all discussed above

刘奇
  • Engine---
    Responsible for the compilation and execution process of the entire JavaScript program from beginning to end.

  • Compiler---
    Friend of the engine, responsible for syntax analysis and code generation.

  • Scope---
    Another friend of the engine, responsible for collecting and maintaining a series of queries consisting of declared identifiers (variables), and enforcing a very strict set of rules that determine the impact of the currently executing code on these identifiers access rights.

When you see var a = 1;
In fact, the engine thinks there are two completely different declarations here, one is processed by the compiler at compile time, and the other is processed by the engine at runtime.
var a=1; decomposition

  1. When encountering var a, the compiler will ask the scope whether there is already a variable with this name in the collection of the same scope. If so, the compiler will ignore the declaration and continue compilation; otherwise it will ask the scope to declare a new variable in the collection of the current scope and name it a.

  2. Next, the compiler will generate the code required for the engine to run at runtime. These codes are used to handle the assignment operation of a=1. When the engine is running, it first asks the scope whether there is a variable called a in the current scope collection. If so, the engine will use this variable; if not, the engine will continue to look for the variable.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template