Destructuring: Array:
let [firstvalue] = [1]; Firstvalue:1
Let C, D;
Let [A, b] = [C, d] = [1, 2];
var [head, ... tail] = [1, 2, 3, 4]; Tail: [2,3,4]
You can swap variables:
let [x, y] = [' ax ', ' why '];
[x, Y] = [y, x]; X:why, Y:ax
Const ALL = [' ax ', ' Why ', ' Zet '];
const [,, z] = all; Z:set
Nested arrays can also:
const USER = [[' Some ', ' one '], 23];
const [[FirstName, surname], age] = user;
for (var [a, b] of [[1, 2]]) {}//a:1, B:2
Strings can also be oh:
Let [A, B, c] = ' abc '; [A, B, c]: [' A ', ' B ', ' C ']
Object
const {T:X} = {t:1, b:2}; X:1
const {X} = {x:1, b:2}; X:1
Const M = {first:23, second:42};
Const {magic: {Second:second}} = {magic:m}; Second:42
const {Z:[,X]} = {z: [23, 42]}; X:42
const [, [{Lang:lang}]] = [NULL, [{env: ' browser ', Lang: ' ES6 '}]; Lang:es6
Including prototype:
const {SUBSTR} = "1"; Substr:String.prototype.subst
Default value:
const [, b=2] = [1,,3]; B:2
const {A, b=2} = {a:1, b:undefined}; B:2
const {A, b=2} = {a:1};
const {X:Y=42} = {y:23}; Y:42
Parameter handling:
CONST FN = ({ID, name}) + = {
// ...
};
Const USER = {name: ' Wolfram ', id:42};
fn (user); Id:42, Name:wolfram
CONST FN = ([, {name}]) = = {
// ...
};
Const users = [{name: ' Nobody '}, {name: ' Alice ', id:42}];
fn (users); Name:alice
You can also use the default values:
CONST FN = (ID, name= ' Bob ', c) = = {
// ...
};
fn (undefined, 3); Id:23, Name:bob, C:3
CONST FN = (ID, name= ' Bobby ') = = {
// ...
};
FN (+, ' Bob '); Id:23, Name:bob
Const DefaultUser = {id:23, name: ' Joe '};
CONST FN = ([User=defaultuser]) + = {
// ...
};
FN ([]); User:defaultuser
CONST FN = (id=1, [arr], {obj=2}) = = {
// ...
};
fn (void 0, [2], {obj:3}); Id:1, Arr:2, Obj:3
Learn some es6:destructuring.