Learn some es6:destructuring.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.