左右两边必须一样???? 也可以不一样
//常规赋值
let arr = [1,2,3]
let a = arr[0]
let b = arr[1]
let c = arr[2]
console.log(a,b,c) // 1 2 3
//解构赋值
let arr = [1,2,3]
let [a,b,c] = arr // [a,b,c] = [1,2,3]
console.log(a,b,c) // 1 2 3
//右边比左边多
let [a,b,c,d] = [1,2,3,'4',5]
console.log(a,b,c,d) //1 2 3 "4"
//左边比右边多
let [a,b,c,d] = [1,2,3]
console.log(a,b,c,d) //1 2 3 undefined
//json对象
let {a,b,c} = {a:1, d:'d', e:3}
console.log(a,b,c) //1 undefined undefined 这个是错的,key 是不一样的
let {a,b,c} = {a:1, b:'b', c:3}
console.log(a,b,c) //1 "b" 3 这个是对的,所以,左右必须一样,key一样
let {c, a, b} = {b:'b', c:3 , a:1}
console.log(a,b,c) //1 "b" 3 这样也对,json对象的key是无序的
//还可以这样,只要key一样就行,左右可以不一样多
let {a,b,c,e,f} = {a:[1,2,3], b:'b is a string', c: {a:1,b:2,c:3}, d:0, e:999}
console.log(a,b,c,e,f)
//(3) [1, 2, 3] "b is a string" {a: 1, b: 2, c: 3} 999 undefined
右边必须是个合法的东西
声明和赋值不能分开
//必须写在一行,下面的是不行的
let [a,b]
[a,b] = [1,2]
console.log(a,b)
//Uncaught SyntaxError: Missing initializer in destructuring declaration