In non-strict mode, there are variables to enhance the features of JavaScript.
1. Cause
JavaScript works is first compiled and then executed during compilation, the interpreter will declare all "moved" to the top where the scope of the assignment or other logic and will remain in place, this is the variable lift.
E.g
foo(); function foo(){ console.log(a); // undefined var a = 2; }
After compilation is understood to be of the form
function foo(){ var a; console.log(a); // undefined a = 2; } foo();
2. Priority function
In the promotion process, it enhances the function declaration will be the first, and then the variable. Only function declarations (function a () {}) will be lifted, a function expression (var a = function b () {}) will not improve. If the same statement is present, the same variable declaration is ignored; the same function declaration, the latter will overwrite the previous.
E.g
foo(); // 1 var foo; function foo(){ console.log(1); } foo = function(){ console.log(2); }
To be understood as
function foo(){ console.log(1); } foo(); // 1 foo = function(){ console.log(2); }