( 1 ) The value of the swap variable
Let x = 1;
Let y = 2;
[x, Y] = [y, x];
Knowledge Point: The deconstruction assignment of an array.
// Deconstruction Assignment of arrays
Let [x,y] = [all];
Let [x,y] = [2];//x = 2, y =undefined;
Let [x = 1] = [undefined];x = 1
Let [x = 1] = null ; X =null
Let [,, third] = ["foo", "Bar", "Baz"]; Third = Baz;
If the right side of the equal sign is not an array (or, strictly speaking, not a ergodic structure, then an error will be reported.)
Error
let [foo] = 1;
let [foo] = false;
let [foo] = NaN;
let [foo] = undefined;
let [foo] = null;
let [foo] = {};
( 2 ) returns multiple values from a function
The function can only return a value, and if you want to return multiple values, you can only return them in an array or object. It is very convenient to take these values out if you have an understanding of the construction assignments.
Returns an array
function Example () {
return [1, 2, 3];
}
Let [A, B, c] = example ();
Returns an object
function Example () {
return {
Foo:1,
Bar:2
};
}
Let {foo, bar} = Example ();
( 3 ) Definition of function parameters
The deconstruction assignment makes it easy to match a set of parameters with the variable name.
A parameter is a set of sequential values
function f ([x, Y, z]) {...}
f ([1, 2, 3]);
Parameter is a set of no-order values
function f ({x, y, z}) {...}
F ({z:3, y:2, x:1});
// Knowledge points
Deconstruction of functions (deconstruction of parameters)
function Move ({x = 0, y = 0} = {}) {
return [x, y];
}
Move ({x:3, y:8}); [3, 8]
Move ({x:3}); [3, 0]
Move ({}); [0, 0]
Move (); [0, 0]
( 4 ) Extract JSON Data
The deconstruction assignment is particularly useful for extracting data from JSON objects.
Let Jsondata = {
Id:42,
Status: "OK",
Data: [867, 5309]
};
Let {ID, status, data:number} = Jsondata;
Console.log (ID, status, number);
"OK", [867, 5309]
Knowledge Points: The deconstruction of objects
There is an important difference between an object's deconstruction and an array. The elements of the array are arranged in order, the value of the variable is determined by its position, and the object's property has no order, and the variable must have the same name as the property in order to get the correct value.
Let {foo:foo, bar:bar} = {foo: "AAA", Bar: "BBB"};
let {Foo:baz} = {foo: "AAA", Bar: "BBB"};
Baz//"AAA"
Foo//Error:foo is notdefined
In the above code, Foo is the matching pattern, and Baz is the variable. The real assignment is the variable Baz, not the note, when P is the pattern, not the variable, so it is not assigned to the value. If P is also assigned as a variable, it can be written as follows.
Let obj = {
P: [
' Hello ',
{y: ' World '}
]
};
Let {p, p: [x, {y}]} = obj;
X//"Hello"
Y//"World"
P//["Hello", {y: "World"}]
Mode foo.
The default value is in effect when the property value of an object is strictly equal to undefined.
var {x = 3} = {x:undefined};
X//3
var {x = 3} = {x:null};
X//NULL
(5) Value and Boolean value of the deconstruction assignment
let {tostring:s} = 123;
s = = = Number.prototype.toString//True
let {tostring:s} = true;
s = = = Boolean.prototype.toString//True
The rule to deconstruct an assignment is to convert it to an object, as long as the value to the right of the equals sign is not an object or an array. Since undefined and null cannot be converted to objects, they will be given an error when they are deconstructed and assigned.
let {prop:x} = undefined; TypeError
let {prop:y} = null; TypeError
Second: The variable's deconstruction assignment