A quick look: var, let, const

 var reassigned redefined scope belongs: function scope;

 

Let variables declared valid only within the block where the let command, Block scope.

 

const declare a constant read-only, once declared value of the constant can not be changed.

 

Portal: https: //www.runoob.com/w3cnote/es6-let-const.html

 

A comparison: Scope: var: function scope, so the following, var totalPrice global; let, const is a block-level scope, act only if conditions in the range;

= 10. price var; 
var COUNT = 10; 
IF (COUNT>. 5) { var the totalPrice = 0.9 * 10; 
    the console.log ( `the totalPrice var: the totalPrice} {$`); 
} 
the console.log (the totalPrice); // Success output 9 totalPrice here is that global variables can be accessed

    

   

= 10. price var; 
var COUNT = 10; 
IF (COUNT>. 5) { the let the totalPrice = 0.9 * 10; 
    the console.log ( `the let the totalPrice: the totalPrice} {$`); 
} 
the console.log (the totalPrice); // will error (Uncaught ReferenceError: the totalPrice iS not defined) the totalPrice here is to let the block-level variables, external inaccessible

   

 

Comparison II: Scope: The following code only type declarations are the same, var support reassign the totalPrice will be reassigned, and let in the totalPrice, the first global variable, the second block is only acting in stage, Therefore, the final output 0

        was Price = 10; 
        were count = 10;     was total price = 0; 
        if (count> 5) {   were Total Price = 10 * 0.9; 
            console.log ( `was total price: $ {total price} '); // 9 
        } 
        console.log (total price); // 9
    
          

  

        var price = 10;
        var count = 10;
        let totalPrice = 0;
        if (count > 5) {
            let totalPrice = 10 * 0.9;
            console.log(`let totalPrice:${totalPrice}`); //9
        }
        console.log(totalPrice);  //0

  

Comparison three: var, let, const: define, assign, as the difference, when the target const note, although it can not be reassigned, it can modify its attribute value

        name = var "Sun"; 
        var name = "Sun2"; // may be redefined 
        name = "liping"; // can be reassigned 
        console.log ( `var name: $ { name}`); // var name: Liping 

        the let Age = 10; 
        // Age = 20 is the let; // be wrong, can not be redefined 
        age = 22; // may be reassigned 
        console.log ( `let age: $ { age}`); / / the let Age: 22 

        const Sex = "female"; 
        // const Sex = "M"; // be wrong, can not be redefined 
        // sex = "male"; // be wrong, can not be reassigned 
        console.log ( `const sex: $ {sex }`); // F

  

The fourth point: change control attributes:

      Product = {const 
            name: "phone" . price: 5999 
        }; 
        // can modify the attributes 
        Product. . price = 8999; 
        the console.log (the JSON.stringify (Product)); // { "name": "phone", " . price ": 8999 } 
        // if you do not want to change the product to be freeze :( method used to freeze the target) 
        const = product2 Object.freeze ({ 
            name:" phone ", . price: 5999 
        }); 
        . product2 . price = 9999; 
        Console. log (JSON.stringify (product2)); // { "name": " mobile phone", ". price": 5999 }

            





           
        

  

Fifth: the details of the actual application process

    for ( var I = 0; I <. 5; I ++) { 
            the console.log (var `I IS $` {I}); // this is output 0,1,2,3,4 

            the setTimeout (function () { 
                Console .log ( `var i is $ { i}`); // this will output 5. 5 

            }, 1000); 
        } 

        the console.log (I IS $ `` {I}); // output 5 will be 


        the console.log ( `--------------- solve setTimeout digital output 0-4 ------------------------- `-----); 


        for ( the let I = 0; I <. 5; I ++) { 
            the console.log (` the let I IS $ `{I}); // this is output 0,1,2,3, . 4 

            the setTimeout (function () { 
                the console.log ( `the let I IS $` {I}); // this is output 0,1,2,3,4 

            }, 1000); 
        }

  

 Sixth: The same analysis: performing the method when a [1], i = 3 has, so output 3 are, if you want to change the output type can be let 2,3

        var a = [];
        for (var i = 0; i < 3; i++) {
            a[i] = function () { console.log(i) }
        }
        a[1](); //3
        a[2](); //3

 

Seventh: when the block-level elements

       a 10 = var; 
        IF (. 1) { 
            a = 100; // when the block-level element comprising let declarations, to a assignment, only to find a block in the current 
            console.log (a); // therefore given here will 
            let a = . 1; 
            the console.log (A); 
        }

  

Eighth: Variable lift:

        console.log (abc); // undefined, variable lift, the actual parsing: var ABC; the console.log (ABC); ABC = 10; 
        var ABC = 10; 


        // Console (NUM); // be wrong 
        let num = 10; 

        //console.log(filename);// be wrong 
        const filename = "abc.txt";

  

Written in the last: more than just directions for use, Baidu will be able to readily understand, follow the principle of further study

 

Guess you like

Origin www.cnblogs.com/hanliping/p/11298060.html