block-level scope es6--

Hoist 1.var and variable declaration

  Var variables declared in the scope of the function or global scope, as will be variable in the current scope top of the statement

  Example:

    function getValue(condition) {           

      if (condition) {                

        var  value = "blue";               

        return value;                  

      } else {                        

        // here to access the variable value, its value is undefined                 

        return null;                             

      }                                   

      // here to access the variable value, its value is undefined         

    }

  Above is equivalent to:

    function getValue(condition) {  

      was value;         

      if (condition) {                

        value = "blue";               

        return value;                  

      } else {                        

        // here to access the variable value, its value is undefined                 

        return null;                             

      }                                   

      // here to access the variable value, its value is undefined         

    }

 

 

2. block-level declaration

  1) and let variables declared const, can limit the scope of the variables in the current block (a. Internal functions, b. The block (region between the character and {})), the variable declaration will not improve and , outside the scope of the definition of scope can not access variables

   

  Example:

    function getValue(condition) {                       

      if (condition) {

        // This is a temporary dead zone (Temporal Dead Zone), use console.log (typeof value) statement will raise an error. Because JavaScript engine found in the variable declaration scan code, or elevate them to the top of the scope of (experiencing var statement), either in the statement put TDZ (and let encountered const statement). TDZ access the variables will trigger a runtime error. Only variable declaration statement is executed, the variable will be removed from the TDZ, and then before normal access.

        let  value = "blue";

        return value;

      } else {

        // variable value does not exist here

        return null;

      } 

      // variable value does not exist here

    }

 

  2) Prohibition redeclarations: let the same scope or the const can not duplicate definition already existing identifier

    例: where count = 30;

      // throw a syntax error

       let  count = 40;

  

  3) const statement

    const declaration is constant, once its value is set can not be changed. Therefore, each must be initialized by constants declared by const

      例: const maxItems = 30;

        const name; // syntax error: uninitialized constant

    const statement and let declarations are very different one, that is not a constant const defined assignment again, otherwise it will throw an error ;

    const not allowed to modify the binding declaration, but allows you to modify the value. I.e. the objects declared const, can modify the attribute values of the object .

 

3. Block scope binding loop

  was funcs = [];

  for (let i = 0; i <10; i ++) {// NOTE: If the declared const here, an error is thrown. Because in the first iteration of the loop, i is 0, the iteration executed successfully. And then do i ++, because this statement attempts to modify constants, and therefore throw an error.

    funcs.push (function () {      So, if the variable is not modified subsequent cycle, it can be declared const, i.e., when the for-in behavior or for-of the loop using the const let the same.

      console.log(i);

    });

  }

  funcs.forEach(function(func) {

    func (); // output 0, then 1, 2, until 9; var if a defined variable output 10 10

  }

 

 

4. The scope of global block binding

  When var is used for global scope, it will create a new global variable as a global object (window Object Browser environment) of the property, which means using var and global properties may inadvertently overwrite an existing ;

  And let or use const, will be created under a new binding global scope, but it does not add to the binding properties of the global object, in other words, with let or const can not override the global variable, but only masking it ;

  There are a = 1;

  let  b = 2;

  console.log(a);      // 1

  console.log(window.a);  //  1

  console.log(b);      // 2

  console.log(window.b);   // undefind

 

 

Summary: The current best practice is to use block-level bindings: Use let the default use const, only really need to change the value of a variable. This can be accomplished to some extent immutable code, so as to prevent some errors.

    

Guess you like

Origin www.cnblogs.com/wxl99180426/p/12104629.html