Tag: The same color EFI tells the assignment code that the ONS codes are not
Deconstruction assignment of an object
The deconstruction assignment of an object is similar to the destructor assignment of an array, so let's look at a small code:
var {a,b,c} = {"A": 1, "B": 2, "C": 3}; Console.log (a);//Result: A has a value of 1 console.log (b);//Result: B has a value of 2 console.log (c);//Result: The value of C is 3
The code for this example is similar to the destructor assignment of an array, except that the array is replaced with an object. But there is a difference between the two, and we make a slight change to the above code:
var {a,b,c} = {"A": 1, "C": 3, "B": 2}; Console.log (a);//Result: A has a value of 1 console.log (b);//Result: B has a value of 2 console.log (c);//Result: The value of C is 3
I swapped the position of object property B and property C on the right side, but this does not affect the result of the assignment, and the value of variable b and variable C does not change, still B is 2,c 3. This tells us that the object's deconstruction assignment is not affected by the order of the attributes (the array is affected), it is associated with the property name, and the variable name is consistent with the property name before the value is successfully assigned.
If a variable cannot find a property with the same name, the assignment is unsuccessful, as in the following example:
var {a} = {"B": 2}; Console.log (a);//Result: The value of a is undefined
Variable A does not find the property A that matches the name on the right, so the variable A is not successful and the value of a is undefined.
But it's not completely out of the way, if you want to give a variable name that is different from the property name, you can write:
var {b:a,} = {"B": 2}; Console.log (a);//Result: A has a value of 2
Deconstruction assignment of ES6 array (bottom)