# 【ES6】 变量的解析赋值

### 1、数组的解构赋值

#### 基本语法

ES6允许按照一定模式，从数组和对象中提取值，对变量进行赋值，这被称为解构。

let [foo,[[bar],baz]] = [1,[[2],3]];
foo;   //1
bar;   //2
baz;   //3

let [ , , third] = [1,2,3];
third;   //3

let [x, , y] = [1,2,3];
x;   //1
y;   //3

tail;   //[2,3,4]

let [x, y, ...z] = ['a'];
x;   //'a'
y;   //undefined
z;   //[]

let [foo] = [];
let [bar, foo] = [];

let [x,y] = [1,2,3];
x;   //1
y;   //2

let [a,[b],d] = [1,[2,3],4];
a;   //1
b;   //2
d;   //4

let [x,y,z] = new Set([1,2,3]);
x;   //1
y;   //2
z;   //3

#### 默认值

let [a = true] = [];
a;   //true

let [x,y = 'b'] = ['a',undefined];
x;   //'a'
y;   //'b'

ES6内部使用严格相等运算符（===），判断一个位置是否有值。所以，只有当一个数组成员严格等于undefined，默认值才会生效。

let [x = 1] = [undefined];
x;   //1

let [y = 1] = [null];
y;   //null

function f(){
console.log('aaa');
}
let[x = f()] = [1];
x;   //1

let x;
if([1][0] === undefined){
x = f();
}else {
x = [1][0];
}

### 2、对象的解构赋值

let{x,y} = {x:"aaa",y:"bbb"};
x;   //"aaa"
y;   //"bbb"

let{z} = {x:"aaa",y:"bbb"};
z;   //undefined

let {ooo:ppp} = {ooo:111,ppp:222};
ooo;   //报错
ppp;   //111

let obj = {first : 'hello', last: 'world'};
let {first:f, last:l} = obj;
f;   //'hello'
l;   //'world'

let {foo: foo, bar: bar} = {foo: "aaa",bar: "bbb"};

let obj = {
p: [
'Hello',
{y:'World'}
]
};
let {p: [x,{y}]} = obj;
x;   //"Hello"
y:   //"World"

let obj = {
p: [
'Hello',
{y:'World'}
]
};
let {p, p:[x,{y}]} = obj;
p;   //["Hello",{y: "World"}]
x;   //"Hello"
y;   //"World"

let x;
{x} = {x:1};   //报错

let a;
({a} == {a: 1});
a;   //1

### 3、字符串的解构赋值

const [a1,b1,c1,d1,e1,f1] = "hello";
a1;   //"h"
b1;   //"e"
c1;   //"l"
d1;   //"l"
e1;   //"o"
f1;   //undefined

let {length : len} = 'hello';
len;   //5

### 4、数值和布尔值的解构赋值

let {toString: s} = 123;
s === Number.prototype.toString;   //true

let {toString: s} = true;
s === Boolean.prototype.toString;   //true

### 5、函数参数的解构赋值

function move({x = 0, y = 0} = {}){
return [x,y];
}
move({x: 3, y: 8});   //[3,8]
move({x: 3});   //[3,0]
move({});   //[0,0]
move();   //[0,0]

function move({x, y} = { x: 0,y: 0}){
return [x,y];
}
move({x: 3, y: 8});   //[3,8]
move({x: 3});   //[3, undefined]
move({});   //[undefined, undefined]
move();   //[0,0]

undefined就会触发函数参数的默认值。

[1,undefined,3].map((x = 'yes') => x);
//   [1, "yes", 3]

### 6、用途

（1）可用于交换变量的值。

let x = 1;
let y = 2;
[x, y] = [y, x];
x;   //2
y;   //1
（2）从函数返回多个值

function example(){
return [1,2,3];
}
let [a,b,c] = example();
[a,b,c];   //[1,2,3]

function example(){
return {
foo:1,
bar:2
}
}
let {foo,bar} = example()
foo   //1
bar   //2


（3）函数参数的定义

//参数是一组有序的值
function f([x,y,z]) { ... }
f([1,2,3])

//参数是一组无序的值
function f({x,y,z}) { ... }
f({z: 3, y: 2, x: 1})

（4） 提取JSON数据

let jsonData = {
id: 42,
status: "OK",
data: [867, 5309]
};
let { id, status, data: number } = jsonData;
console.log(id, status, number);   // 42 "OK" (2) [867, 5309]

（5）函数参数的默认值

jQuery.ajax = function (url, {
async = true,
beforeSend = function () {},
cache = true,
complete = function () {},
crossDomain = false,
global = true,
// ...
} = {}) {
// ...
};

（6）遍历Map结构

const map = new Map()
map.set('first','hello')
map.set('second','world')
for(let [key, value] of map){
console.log(key + ' is ' + value)
}
// first is hello
// second is world

// 获取键名
for(let [key] of map){
// ...
}

// 获取键值
for(let [,value] of map){
// ...
}

（7）输入模块的指定方法

const { SourceMapConsumer, SourceNode } = require('source-map')

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120