Definition: ES6 allows you to extract values from arrays and objects and assign values to variables in a pattern called deconstruction.

Destruct assignment of arrays

1. Common usage: let [a, b, c] = [1, 2] / / the result: a = 1, b = 2, c = 2 undefined. Let [foo,[[bar],baz]]=[1,[[2],3]; / / results: foo = 1, bar = 2, baz = 3 3. Give a default value: let [a, b, c = 'default'] = [1, 2] 4. Let [, and third] = [" foo ", "bar", "baz"]. / / results: third = "baz" let (x, y) = [1, 2, 3] / / results: x = 1, y = 3 let [a head, tail...] = [1, 2, 3, 4]; / / the result: head = 1, tail = [4] 2 let [x, y,... 'z'] = [' a '] / / results: x = 'a', y = undefined, z = []Copy the code
1. If the structure is unsuccessful, the value of the variable is equal to undefined. 2Copy the code

Object destructuring assignment

Let {Miao, Ketang}={Miao :1, Ketang :2} Let {Miao =100,ketang}={ketang:2} Default value Let {Miao: Leo, Ketang}={ketang:2} alias var miaov ={ fn:function(){}, add:function(){alert('111')}, isFunction:function(){} } let {fn,add,isFunction,isArray=function(){}}= miaov; cosnole.log(fn);Copy the code

· Destruct assignment of strings

var [a,b,c]  = 'hello';
Copy the code

· Deconstructed assignment of function parameters

function fn({a,b,c,d='get'}){
  console.log(a);
   console.log(b);
   console.log(c);
   console.log(d);
}
fn({a:1,b:2,c:3})
Copy the code

Template string

Definition: Template strings are enhanced strings, identified by backquotes. Write the expression in the placeholder ${}

var miaov = 'ketang'; let str=`<ul> <li>${miaov}</li> <li>${1+1}</li> <li>${true? 1:2}</li> </ul>`Copy the code