ES6 (ii) extension of deconstruction Assignment + string

Source: Internet
Author: User

Explain the previous deconstruction assignment

① "..." in the assignment of the deconstruction.

Let [a,... b]= [1];     
b //[] ... Represents the variable B to match all remaining elements returns an array that is not matched to return []
Note: "... b" can only be put in the last

② The data type must be the same on both sides of an equal-value solution assignment

That

let [] = [] or let {} = {}

However: The set structure also allows the use of arrays for the assignment of destructors

New Set ([1,2,3,4]) a  //1b  //2

Tip: If you're not sure if the structure can be deconstructed, determine if it has a iterator interface. The judging method is as follows:

function* fibs () {  var x= 0;   var y= 1;    while (true) {    yield x;     = [Y, x+ y];  }} var [A, B, C, D, E, f] = fibs (); F//  5//   fibs is a generator function, Native has a iterator interface. The deconstruction assignment gets the value from this interface in turn. 

③ Deconstruction assignment applies not only to let/const but also to Var directives

④ refactoring assignment allows default values to be added

// 4

(The matching pattern of the deconstructed assignment is = = =) Note: The default value does not take effect if the deconstruction assignment is not strictly equal to undefined

let [a = 4]=//4
let [b = 4]=[null]b//null

⑤, I've made my own mistakes.

let [x = y, y = 1] = [];     // error  because x=y when Y has not been declared    will not be promoted!!! Remember
var [a = b, b = 1] = []; a===undefined b = = 1

Deconstruction assignment of an object

① objects are not ordered in the same way as arrays, so the object's deconstruction assignment is matched by the ' key '.

var {Foo:baz} = {foo: ' aaa ', bar: ' BBB '//  ' aaa '      foo is a matching pattern  baz is a variable

② object and array nesting if you look at the example, it's almost there.

var obj = {  p: [    ' Hello ',    ' World ' }  }; var {p: [x, {y}]} = obj;      //p is a match pattern  [x, {y}] is a value   then look at each item in the array
// "Hello"
//

③ returns an undefined object when it does not exist as an array.

Deconstruction assignment of a string

const [A, B, C, D, e] = ' Hello ';  A  //  hB  //  eC  //  L

let {length : len} = ‘hello‘;len // 5 匹配的是字符串的length属性

Dry Goods: Use

(1) value of the swap variable 

[A,b]=[b,a]

(2) Returning multiple values from a function

function example () {  return [1, 2, 3];} var [A, B, c] = example ();

(3) Definition of function parameters

function f ([x, Y, z]) {...} f ([1, 2, 3]);

(4) Extracting JSON data

var =  {jsondata,  "OK",  data: [867, 5309 = Jsondata;

(5) Default value for function parameters

function f ([x = 1, y = 2, z]) {...}

(6) Traverse the map structure

var New Map (); Map.set (' first ', ' Hello '); Map.set (' second ', ' World ') ;  for (Let [key, value] of the map)  {+ "is" + value);} // First is Hello // second is world

//get key name for  (let [key" of Map) {//... }//get key value for  (let [ ,value] of Map{//...              /span>          

(7) Specify method of input module

const {Sourcemapconsumer, Sourcenode} = require ("Source-map");

  

ES6 (ii) extension of deconstruction Assignment + string

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.