es6入门笔记-解构赋值

es6中关于数组解构、对象解构、字符串解构、数值和布尔值解构、函数参数的解构以及其应用场景。

数组的解构赋值

基本用法

1
let [a, b, c] = [1, 2, 3];

解构不成功时,变量的值就等于undefined。

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

解构成功还会出现不完全解构的情况

1
2
3
4
5
6
7
let [x, y] = [1, 2, 3];
x // 1
y // 2
let [a, [b], d] = [1, [2, 3], 4];
a // 1
b // 2
d // 4

解构赋值允许指定默认值。

1
2
3
4
let [x = 1] = [undefined];
x // 1
let [x = 1] = [null];
x // null

对象的解构赋值

基本用法

1
2
3
let { foo, bar } = { foo: "aaa", bar: "bbb" };
foo // "aaa"
bar // "bbb"

变量名与属性名一致时

1
2
3
4
let { bar, foo ,baz } = { foo: "aaa", bar: "bbb" };
foo // "aaa"
bar // "bbb"
baz // undefined

变量名与属性名不一致时

1
2
3
let { foo: baz } = { foo: 'aaa', bar: 'bbb' };
baz // "aaa"
foo // error: foo is not defined

上个例子出现foo不能识别的情况,原因在于foo是匹配的模式,baz才是变量,下例中loc是模式,也可以变量赋值。

1
2
3
4
5
6
7
8
9
10
11
12
const node = {
loc: {
start: {
line: 1,
column: 5
}
}
};
let { loc, loc: { start }, loc: { start: { line }} } = node;
line // 1
loc // Object {start: Object}
start // Object {line: 1, column: 5}

默认赋值解构。对象的属性值严格等于undefined时,取默认值。解构失败也会等于undefined

1
2
3
4
5
6
7
8
9
var {x: y = 3} = {x: 5};
y // 5
var {x = 3} = {x: undefined};
x // 3
var {x = 3} = {x: null};
x // null
let {foo: {bar}} = {baz: 'baz'};
foo //报错,foo为undefind,其bar属性不能获取
//相当于let _tmp = {baz: 'baz'}; _tmp.foo.bar

字符串的解构赋值

字符串也可以解构赋值。这是因为此时,字符串被转换成了一个类似数组的对象。

1
2
3
4
5
6
const [a, b, c, d, e] = 'hello';
a // "h"
b // "e"
c // "l"
d // "l"
e // "o"

类数组存在length属性,可以通过该属性解构

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

数值和布尔值解构赋值

注意:解构赋值时,如果等号右边是数值和布尔值,则会先转为对象。

1
2
3
4
5
let {toString: s} = 123;
s === Number.prototype.toString // true

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

注意:解构赋值的规则是,只要等号右边的值不是对象或数组,就先将其转为对象。

1
2
let { prop: x } = undefined; // TypeError
let { prop: y } = null; // TypeError

函数参数的解构赋值

默认传参

1
2
[[1, 2], [3, 4]].map(([a, b]) => a + b);
// [ 3, 7 ]

函数默认传参

1
2
3
4
5
6
7
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
2
3
4
5
6
7
8
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]
move({x: undefined, y: null}) // [0,null]

用途

  • 交换变量
1
2
3
let x = 1;
let y = 2;
[x, y] = [y, x];
  • 从函数返回多个值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 返回一个数组
function example() {
return [1, 2, 3];
}
let [a, b, c] = example();

// 返回一个对象
function example() {
return {
foo: 1,
bar: 2
};
}
let { foo, bar } = example();
  • 函数参数的定义
1
2
3
4
5
6
7
// 参数是一组有次序的值
function f([x, y, z]) { ... }
f([1, 2, 3]);

// 参数是一组无次序的值
function f({x, y, z}) { ... }
f({z: 3, y: 2, x: 1});
  • 提取JSON数据
1
2
3
4
5
6
7
8
9
let jsonData = {
id: 42,
status: "OK",
data: [867, 5309]
};

let { id, status, data: number } = jsonData;

console.log(id, status, number); // 42, "OK", [867, 5309]
  • 函数参数的默认值
1
2
3
4
5
6
7
8
9
10
11
jQuery.ajax = function (url, {
async = true,
beforeSend = function () {},
cache = true,
complete = function () {},
crossDomain = false,
global = true,
// ... more config
}) {
// ... do stuff
};
  • 遍历Map结构
1
2
3
4
5
6
7
8
9
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
  • 输入模块的指定方法
1
const { SourceMapConsumer, SourceNode } = require("source-map");

参考链接
http://es6.ruanyifeng.com/#docs/destructuring

------本文结束 感谢阅读------