1、解构赋值的理解
以前对变量赋值只能为变量指定赋值,es6允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构。
是一种针对数组或者对象进行模式匹配,然后对其中的变量进行赋值,方便了复杂对象中数据字段的获取。
2、解构赋值的应用场景
(1)交换变量
let x = 1;
let y = 2;
[x, y] = [y, x];
(2)从函数返回多个值
// 返回一个数组
function example() {
return [1, 2, 3];
}
let [a, b, c] = example();
// 返回一个对象
function example() {
return {
foo: 1,
bar: 2
};
}
let { foo, bar } = example();
(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", [867, 5309]
(5)函数参数使用默认值
jQuery.ajax = function (url, {
async = true,
beforeSend = function () {},
cache = true,
complete = function () {},
crossDomain = false,
global = true,
// ... more config
} = {}) {
// ... do stuff
};
(6)遍历 Map 结构
// 获取键名
for (let [key] of map) {
// ...
}
// 获取键值
for (let [,value] of map) {
// ...
}
(7)输入模块的指定方法
const { SourceMapConsumer, SourceNode } = require("source-map");
3、解构赋值的使用方法
------------数组解构赋值-----------------------------------------------------
let [foo, [[bar], baz]] = [1, [[2]]]; //可嵌套可忽略
foo // 1
bar // 2
baz // undefined
let [ , , third] = ["foo", "bar", "baz"];
third // "baz"
let [x, , y] = [1, 2, 3];
x // 1
y // 3
let [head, ...tail] = [1, 2, 3, 4]; //剩余运算符
head // 1
tail // [2, 3, 4]
let [x, y, ...z] = ['a'];
x // "a"
y // undefined ,解构不成功则为undefined
z // []
let {a = 10, b = 5} = {a: 3}; //let中声明有默认值
// a = 3; b = 5;
let {a: aa = 10, b: bb = 5} = {a: 3};
// aa = 3; bb = 5;
let [x, y] = [1, 2, 3]; //不完全解构是只匹配等号右边的数据
x // 1
y // 2
let [a, b, c, d, e] = 'hello'; //解构的目标若为可遍历对象,皆可进行解构赋值。可遍历对象即实现 Iterator 接口的数据
// a = 'h'
// b = 'e'
// c = 'l'
// d = 'l'
// e = 'o'
let [x = 1] = [null]; //null不严格等于===undefined,所以不会使用默认值
x // null
function f() {
console.log('aaa');
}
let [x = f()] = [1]; //默认值是一个表达式时,只有触发使用默认值时才执行表达式
let [x = y, y = 1] = []; // 错误原因:x用y做默认值时,y还没有赋值
----对象解构赋值,变量的取值和赋值次序无关,赋值的属性名必须和变量名一致时赋值。----------------
let { bar, foo ,baz} = { foo: 'aaa', bar: 'bbb' };
foo // "aaa"
bar // "bbb"
baz // "undefined",解构失败则变量的值等于undefined
let { log, sin, cos } = Math; //方便地将现有对象的方法,赋值到某个变量。
let { foo: baz } = { foo: 'aaa', bar: 'bbb' }; //变量名和属性名不一致的使用方法
baz // "aaa" ,是先找到同名属性foo,然后再赋给对应的变量baz,被赋值的是:后的baz变量。
let obj = {
p: [
'Hello',
{ y: 'World' }
]
};
let { p, p: [x, { y }] } = obj; //嵌套结构的对象
x // "Hello"
y // "World"
p // ["Hello", {y: "World"}]
let {foo: {bar}} = {baz: 'baz'}; //错误原因:左侧的foo属性undefined
var { message: msg = 'Something went wrong' } = {}; //对象默认值
msg // "Something went wrong"
let x;
{x} = {x: 1}; // 错误原因:已声明变量用于结构赋值,JavaScript 引擎会将{x}理解成一个代码块,从而发生语法错误。
({x} = {x: 1}); //正确写法
let arr = [1, 2, 3]; //数组本质是特殊对象,可以对数组进行对象属性的解构
let {0 : first, [arr.length - 1] : last} = arr;
first // 1 ,0键对应的值是1
last // 3
const [a, b, c, d, e] = 'hello'; //字符串被转换成了一个类似数组的对象
a // "h"
b // "e"
let {toString: s} = 123; //等号右边的值不是对象或数组,就先将其转为对象。
s === Number.prototype.toString // true
let { prop: x } = undefined; // TypeError,undefined 和null无法转为对象会报错
let { prop: y } = null; // TypeError
function add([x, y]){
return x + y;
}
add([1, 2]); // 3,函数参数解构
function move({x = 0, y = 0} = {}) { //函数参数解构使用默认值
return [x, y];
}
move({x: 3, y: 8}); // [3, 8]
move({x: 3}); // [3, 0]
move({}); // [0, 0]