解构赋值其实就是从数组和对象中提取值json
数组的解构赋值
这种写法属于“模式匹配”,只要等号两边的模式相同,左边的变量就会被赋予对应的值。数组
let [a,b,c] = [1,2,3]; let [foo, [[bar], baz]] = [1, [[2], 3]]; //foo 1 //bar 2 //baz 3 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 z // [] // 若是解构不成功,那么值就是undefined
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 [foo] = 1; let [foo] = false; let [foo] = NaN; let [foo] = undefined; let [foo] = null; let [foo] = {};
能够设置默认值函数
let [foo= 1] = [2] // foo 2 let [foo=1] = [undefined] // foo 1 let [foo=1] = [null] // foo null
function f() { console.log('aaa'); } let [x = f()] = [1] // 函数是具备惰性求值的,若是是能够取到值就不会执行的,所以x=1
默认值能够引用解构赋值的其余变量,但该变量必须已经声明。ui
let [x = 1, y = x] = [] // x=1 y=1 let [x = 1, y = x] = [2] // x=2 y=2 let [x = 1, y = x] = [1, 2]; // x=1; y=2 let [x = y, y = 1] = []; // ReferenceError: y is not defined
对象的解构赋值
其实对象的解构也跟数组差不了多少code
let { foo, bar } = { foo: "aaa", bar: "bbb" }; foo // "aaa" bar // "bbb" 对象解构于数组的最大的不一样是,数组讲究的是次序的对应,而对象解构则是key于变量的对应 let { bar, foo } = { foo: "aaa", bar: "bbb" }; foo // "aaa" bar // "bbb" let { baz } = { foo: "aaa", bar: "bbb" }; baz // undefined
若是变量名和属性名不一致对象
let { foo: baz } = { foo: 'aaa', bar: 'bbb' }; baz // "aaa" let obj = { first: 'hello', last: 'world' }; let { first: f, last: l } = obj; f // 'hello' l // 'world' // 对象解构内部的机制实际上是先找到同名的属性,而后再赋值给对应变量,真正被赋值的是右侧,不是前者。
嵌套解构的对象也能够解构ip
let obj = { p: [ 'Hello', { y: 'World' } ] }; let { p: [x, { y }] } = obj; x // "Hello" y // "World" // 这时p是模式,不是变量,所以不会被赋值。
let obj = { p: [ 'Hello', { y: 'World' } ] }; let { p, p: [x, { y }] } = obj; x // "Hello" y // "World" p // ["Hello", {y: "World"}] // 这样p就是变量了
对象解构能够指定默认值字符串
var {x = 3} = {}; x // 3 var {x, y = 5} = {x: 1}; x // 1 y // 5 var {x: y = 3} = {}; y // 3 var {x: y = 3} = {x: 5}; y // 5 var { message: msg = 'Something went wrong' } = {}; msg // "Something went wrong"
// 默认值生效的条件是 let {foo =3} = {foo:undefined} let {foo = 3} = {foo : null} // foo null
// 若是对于嵌套属性的解构,若是子对象所在的父级的属性不存在,则会报错 // 报错 let {foo: {bar}} = {baz: 'baz'};
已经声明的变量用于解构赋值io
let x; {x} : {x: 1}; // 报错 // 上面代码的写法会报错,由于 JavaScript 引擎会将{x}理解成一个代码块,从而发生语法错误。只有不将大括号写在行首,避免 JavaScript 将其解释为代码块,才能解决这个问题。 // 正确的写法 let x; ({x} = {x: 1});
// 有一个很实用的小例子 let {log, sin , cos} = Math;
字符串的解构赋值
const [a, b, c, d, e] = 'hello'; a // "h" b // "e" c // "l" d // "l" e // "o" 相似数组的对象都有一个length属性,所以还能够对这个属性解构赋值。 let {length: len} = 'hello'; len: 5
函数参数的解构赋值
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] 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]
解构赋值的用途
交换变量console
let x = 2; let y = 3; [x, y] = [y, x];
从函数返回多个值
function foo() { return [2, 3]; } let [a, b] = foo(); function bar() { return { foo: 1, bar: 2 } } let {foo,bar} = bar();
函数参数的定义
// 参数是一组有次序的值 function f([x, y, z]) { ... } f([1, 2, 3]); // 参数是一组无次序的值 function f({x, y, z}) { ... } f({z: 3, y: 2, x: 1});
提取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]
引入模块的对应
import {function1, function2} from '../' const {fun1, fun2 } = require('../')
遍历 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) { // ... }