var tmp = new Date();
function f() {
console.log(tmp);
if (false) {
var tmp = 'hello world';
}
}
f(); // undefined
var tmp = new Date();
function f() {
var tmp;
console.log(tmp);
if (false) {
tmp = 'hello world';
}
}
f(); // undefined
var only has function scope, the query process is regarded as scope first, if not continue to look up!
refer to https://codeshelper.com/a/11...
.
during the call to a function, the execution context is created and the life cycle is divided into two phases, creation and execution.
creation phase: the execution context creates variable objects, establishes scope chains, and determines the direction of the this, respectively.
execution phase: variable assignments, function references, and other code execution are completed.
that is, if a variable declaration is found during the creation phase, an attribute is created with the variable name and the value undefined. The execution phase is really assigned.
Source: Front-end basic advance (3): detailed explanation of variable objects
you already know the concept of ascension, but you don't know the answer yet. Promotion occurs in the parsing phase, no matter whether the code is executed or not, the promotion will occur
pre-compilation, there is a quartet, you will understand.