Relationship and influence between function declaration, variable declaration and variable assignment in JavaScript
There are several common understandings between function declarations, variable declarations, and variable assignments:
1. All global variables are properties of window
2. The function declaration is promoted to the top of the range scope
3. Variable declarations are promoted to the top of the range scope
4, the variable declaration is higher precedence than the function declaration, the variable declaration takes precedence over the function declaration is promoted, if both the same name exists simultaneously, the promoted function declaration overrides the first promoted variable declaration
5, the variable assignment will not be promoted, until the execution of the line code to start assigning values
Add:
6, the entire process of invoking JavaScript functions can be divided into the pre-compilation period (also called the Declaration period) and the assignment period (also known as the calculation of the execution period).
预编译期完成对所有变量(包括形参、函数内部参数)和函数的声明,赋值期又可以分为两个阶段,函数花括号内语句执行前后,执行前完成对形参置和函数的赋值,执行后按照语句顺序执行赋值。
For example:
function test(msg){ var msg ; alert(msg);//弹出:test方法测试,因为先完成所有变量的声明,然后在对形参赋值}test("test方法测试");
The summary and test code of the above content can be viewed:
http://ju.outofmemory.cn/entry/4646
Original: http://dmitry.baranovskiy.com/post/91403200
Relationship and influence between function declaration, variable declaration and variable assignment in JavaScript