JavaScript must be knowledge - to enhance the scope write

What upgrade?

The declaration of variables and functions moved to the top in which to write scopes, is called lift .

The following code:


console.log(name); // undefined
var name = 'Rewa Fang';
console.log(name); // Rewa Fang 

The first print name, the output undefinedrather than throwing an exception ReferenceError. It is because variable namedeclaration is promoted. But only to enhance the statement, the assignment will not improve; the output undefined. When the second printing, namehas been assigned Rewa Fang.

After lifting the code:

var name;
console.log(name); // undefined
name = 'Rewa Fang';
console.log(name); // Rewa Fang 

This code is written into the following is normally performed:


name = 'Rewa Fang';
console.log(name); // Rewa Fang 
var name;

var name; It will be raised to the top.

This is because the compiler can find the code at compile all of the statements , and bind the corresponding scope. The code assignments and other logic to remain in place; awaiting execution. For example:  var a = 1; will be seen as two parts of the compiler declaration var a;and assignment a = 1; , then the statement will be raised to the top of the scope of the assignment is performed in place and so on.

Contains function declarations will be raised.

Enhance the function declaration


sayHi(); // Hello!
function sayHi(){
    console.log('Hello!');
}

The sayHi function () can be performed normally; declared as part of the function is promoted. Upgraded to:


function sayHi(){
    console.log('Hello!');
}
sayHi(); // Hello!

Internal function variables and functions will be raised to the top of the function:

var name = 'Lebron James';
sayHi(); // Hello! Rewa Fang
function sayHi(){
    name = 'Rewa Fang';
    console.log('Hello! '+name);
    var name;
}

name within the function will be to enhance the scope function to create the top, so the internal function does not refer to external name. Inside the external variable name name obscured.

After the upgrade:


function sayHi(){
    var name;
    name = 'Rewa Fang';
    console.log('Hello! '+name);
}
var name;
name = 'Lebron James';
sayHi(); // Hello! Rewa Fang

There is a change after the upgrade is a function declaration will give priority to the variable lift .

such as:


console.log(sayHi);

var sayHi = 'Lebron James';

function sayHi(){
    console.log('Hello! ');
}

The result output: ƒ sayHi(){console.log('Hello! ');} the output at node environment:[Function: sayHi]

why ?

Why upgrade?

For the following reasons:

  • Optimize performance; the compiler will compile a pre-treatment variables and functions statement before the code is run, unified management scope. Keep the code from top to bottom in the order of variables declared before being referenced. But the order of the code can be artificially controlled, like Java developers do not need to upgrade to effectively manage declare good variables. So this may not be the most important reason for the upgrade, it may be a historical legacy.
  • Function calls between each other; the following code, if not enhance the function b; a function call would throw an exception; you can make to enhance the functional programming more flexible.

    
     function a (){
        var a = 1;
         return b(a);
     }
    
     function b(num){
        return num * 2;
     }
    

PS: ES6 will not be promoted in let const

Guess you like

Origin blog.51cto.com/14436164/2417734