1. The js engine runs js in two steps: pre-parsing and code execution
(1) Pre-parsing: The js engine promotes all vars and functions in js to the front of the current scope
(2) Code execution: execute in the order in which the code is written
2. Pre-parsing is divided into: variable pre-parsing (variable promotion) and function pre-parsing (function promotion)
(1) Variable promotion, all variable declarations are promoted to the front of the scope, but the assignment operation is not promoted
(2) Function promotion is to promote all function declarations to the front of the current scope without calling functions
like:
fun()
var fun = function(){
console.log(00)
}//Error
//Equivalent to execution
was fun
fun()
fun = function(){
console.log(00)
}
an implementation example
function f1(){
var a=b=c=10; //相当于是var a=10;b=10;c=10 bc是全局变量
console.log(a)
console.log(b)
console.log(c)
}
f1()
console.log(c)
console.log(b)
console.log(a)
result: