ES6 variable and Deconstruction (b)

A statement using the variable [ test sample to be tested in a node environment, the browser environment is not fully compatible with the code ES6 ]
ES6 {} may be used to include any piece of code, the contents of the package {} is called a Code block ( local scope )

the let keyword  [ declare variables ]
characteristics:
1. block-level scope [ locally in the block of statements ]
2. [variable declarations not improve variables can not be used before the variable is not declared ]
3. [temporary dead zone in front of the variable declaration area ]
4. statement [not repeat the same block of code can not repeat the same variable declarations }

const keyword     [ declared constants (generally denotes a constant capital letters )]
characteristics: [ in compliance let declarations add the following two characteristics in the characteristic variable ]
1, [initialization declaration statement must be assigned the same time ]
2, and is not editable

two deconstruction
1, deconstruction array assignment [ left is variable, the right is the value, the value is undefined when no match left ]
totally deconstruct [ about exactly match the data ]
eg:

    let [a,b,c] = [1,2,3];


Incomplete deconstruction [ about data from different ]
a, left more than data only declare the variable whose value is undefined
b, the right side of the extra data entry is ignored without considering
eg:

    let [a, [b], d] = [1, [2, 3], 4];     //a = 1; b = 2; d = 4 


Deconstruction set { extended operator using ... ]
a, ... tail current returns an array of all composition values right unmatched
eg:

    the let [head, tail ...] = [. 1, 2,. 3,. 4]; // head =. 1; tail = [2,. 3,. 4] 
    // accumulate operation 
    the let SUM = 0 ;
     function Test (... ARR) {
         // REST parameters parameters extended operator received in the form of an array is returned parameters ------> is not recommended to use an object arguments array parameters 
        // ARR = [1,2,3,4, . 5] 
        for (the let I = 0; I <arr.length; I ++ ) { 
            SUM + = ARR [I]; 
        } 
    } 
    
    Test ( 1,2,3,4,5);


The default value [ when matched exactly equal to the value undefined, the default value of the entry into force ]
eg:

    let [x, y = 'b'] = ['a'];   // x='a', y='b’


The default value is a function
NOTE: first determine whether the values match, if the matching value is exactly equal undefined, then the default assignment operator value; Otherwise, the default assignment operation is not performed
eg:

    function test() {
        console.log('test');
        return 2;
    }
    let [x = test()] = [];
    console.log(x);        //test    2


    
2, the object assignment deconstruction [left when the right corresponding to the variable property name does not exist, the object attribute value is undefined; i.e., the object not declared attribute is undefined]
Object of the original structure assignment [ after renaming variables, the final declaration of variables rename variables ]
eg:

    {name the let: myName, Age: myAge} = {name: 'NZC', Age: 18 is }
     // The above code is analogous to the left following the code name {matching acquired right of the object property with the same name and attribute values assigned to the name of the renamed myName variable == "to let myName = 'nzc'] 
    let myName = 'nzc' ; 
    the let myAge = 18 is;

   
Properties of the object did not order, variables must be taken only after the property of the same name to the correct value of the [ rename the same can be abbreviated ]
eg:

    {name the let: name, Age: Age} = {name: 'NZC', Age: 18 is }
     // abbreviated as 
    the let {name, Age} = {name: 'NZC', Age: 18 is }
     // analogy to the following codes 
    name = the let 'NZC' ; 
    the let Age = 18 is;


Nested objects deconstruction
eg:

    Person param = {the let: [ 'NZC', {Age: 18 is }]}; 
    the let {param: [name, Age {}]} = Person; // name = 'NZC' Age = 18 is 
    // analogy below param tag is renamed as [name, {age}], it does not declare itself; param variable does not exist i.e. 
    let {param: [name, { age}]} = {param: [ 'nzc', {age: 18} ]}

   
The default value ( condition defaults in effect, the object's property value exactly equal undefined )
EG:

    // name = 'nzchs' -> variable name default value; age: myAge = 21-> myAge Default [age rename myAge then given a default value} 
    let {name = 'nzchs', age: myAge = 21} = name {: 'NZC', Age: 18 is } 
    the let {name = 'nzchs', Age: 21 is myAge =} = {name: 'NZC'}  


    
3, deconstruction string assignment
when deconstruction, the string is converted to a similar array of objects.
eg:

    let [a, b, c] = 'hello'; //a=h;b=e;c=l

length property deconstruction
eg:

    {length the let: len} = 'Hello'; // len = length. 5 [match the right class of the object property array is converted into a character string and its value is assigned to the variable len renamed]

 


4, the numerical and Boolean values destructuring assignment
when deconstructed, if the right hand side is a numerical and Boolean values, will be converted to the corresponding first reference data base type object
eg:

    {toString the let: str1} = 123; // function str1 === Number.prototype.toString returns true 
    the let {toString: str2} = true ; // function returns true str2 === Boolean.prototype.toString



5, the assignment of function parameters deconstruction
basic array parameter passing destructuring assignment
eg:

    function add ([X, Y]) { return X + Y;} 
    add ([ . 1, 2]);    // function returns a value add 3


Function parameters with default values
eg:

    function Test ({X = 0, Y = 0 }) {
         return [X, Y]; 
    } 
    // function calls 
    Test ({X:. 3, Y:. 8}); // return value [. 3,. 8] 
    Test ({X:. 3}); // return value [. 3, 0] 
    Test ({}); // return value is [0, 0] 
    Test (); // error Can not destructure property `x` of ' undefined 'or' null '

   
Third, the common use of deconstruction
1, the exchange value of the variable
eg:

    let x = 1;
    let y = 2;
    [x,y] = [y,x];  
    console.log(x,y); //2 1    

2, the assignment of function parameters:
EG:

    // [A = 0, B =. 1] = [. 1] = A. 1, B =. 1 
    function Test ([A = 0, B =. 1 ]) {
         return A + B; 
    } 
    Test ([ . 1]);   // returns a value of 2

3, extract the data objects
eg:

    ID {obj = the let: 42 is, Status: "the OK", Data: [867, 5309 ]}; 
    the let {ID, Status, Data: Number} = obj;   // define variables corresponding to the

4, the input module designation method

    const { SourceMapConsumer, SourceNode } = require("source-map");


5, traverse the map structure

    var map = new Map();
    map.set('name', 'nzc');
    map.set('age', 18);
    for (let [key, value] of map) {
        console.log(key + " is " + value);    // name is nzc   age is 18
    }

 

Guess you like

Origin www.cnblogs.com/nzcblogs/p/11348883.html