ES6规定:容许按照必定模式,从数组和对象中提取值对变量进行赋值,咱们称之为解构。
之前赋值只能直接指定值
let a = 1; let b = 2; let c = 3;
ES6容许如下这种作法vue
let [a, b, c] = [1, 2, 3];
上面代码表示能够从数组中抽取值赋给对应位置的变量
固然若是解构不成功,变量的值就为undefined
ajax
let [foo] = []; let [bar, line] = [1]; //foo是undefined bar是1 line是undefined
若是等号右边的不是数组(或者说不可遍历的解构),就会报错vuex
//如下都会报错 let [foo] = null; let [foo] = undefined; let [foo] = 1; let [foo] = NaN; let [foo] = false; let [foo] = {};
默认值
数组解构能够赋默认值
当数组解构进行赋值的时候会先判断对应位置上是否有值(判断规则是严格等于===
),若是不存在的话默认值才会生效json
let [foo] = [true];//foo = true; let [x, y = 'b'] = ['a'];//x = 'a'; y = 'b'; let [x, y = 'b'] = ['a', undefined];//x = 'a'; y = 'b'; let [x, y = 'b'] = ['a', null];//x = 'a'; y = null;
若果默认值是一个表达式,那么这个表达式是惰性的,只有在用到的时候才会求值数组
function getFoo() { console.log('log: doing'); return 'foo'; } let [x = getFoo()] = [1];//getFoo并未执行,由于解构的时候发现有对应的值 x = 1
固然默认值能够取已经存在的变量的值,记住是已经存在的。async
let [x = 0, y = x] = [];//x = 0; y = 0; let [x = y, y = 0] = [];//报错由于x须要默认值的时候y还不存在 let [x = y, y = 0] = [1];//x = 1;y = 0;由于x能取到值 因此默认值的赋值操做压根不会执行
第一部分咱们知道数组解构赋值是按照顺序来的,对象就不同了,对象的属性并无顺序,对象的解构赋值是按照属性名来的。
变量名与属性名同样的状况:函数
let {foo, bar, line} = {foo: 'Hello', bar: 'ES6'}; foo //'Hello' bar //'ES6' line//undefined
变量名与属性名不同的状况:url
let {foo: fooTest} = {foo: 'Jason'}; fooTest//'Jason'
固然同名的彻底能够理解成spa
let {foo: foo, bar: bar, line: line} = {foo: 'Hello', bar: 'ES6'};
对象与数组结合后能够组成嵌套模式prototype
let { test: [ name ], mod } = { test: ['Jason'], mod: 'Hello' } mod//'Hello' name//'Jason' test//报错 test is not defined
上个例子中若是想要获取test须要先定义
let { test, test: [ name ], mod } = { test: ['Jason'], mod: 'Hello' } mod//'Hello' name//'Jason' test//['Jason']
一样的对象的解构也能够设置默认值,默认值生效的条件是,对象的属性值严格等于undefined
。
let {x = 0} = {}; let {obj = {}} = {obj: null}; x//0 obj//null
解构失败的话变量被赋予undefined
let { x } = {}; x//undefined
若是解构模式是嵌套的对象,子对象所在的父属性不存在就会报错。
let {user: { name }} = {foo: 'test'}; //报错 由于name的父属性不存在
若是对一个已经声明的变量进行解构赋值必定要注意
let x; { x } = { x: 0 }; //报错 语法错误 JavaScript会把{x}看成代码块
为了不以上的错误应该避免大括号位于行首,咱们应该将解构赋值放在圆括号内。
let x; ({ x } = { x: 0 }); x//0
对象的解构赋值能够很方便的将已有的方法赋值到变量中方便使用
let {sin,cos,log,PI} = Math; sin(PI/6)
由于数组是一种特殊的对象,因此能够对数组使用对象进行解构
let arr = [1, 2, 3]; let {0: test, [arr.length - 1]: name} = arr; test//1 name//3
字符串也能够解构赋值,由于此时字符串会被当作一个相似数组的对象
let [first, , , ,last] = '12345'; first//'1' last//'5'
相似数组的对象都有长度length
属性
let { length } = '12345'; length//5
若果是对数字或者布尔值进行解构,会先将数字或者布尔值转换成对象
let {toString: s} = 123; s === Number.prototype.toString//true
let {toString: s} = true; s === Boolean.prototype.toString//true
特别注意undefined与null没法转换为对象,对他们进行解构都会报错
let { prop: x } = undefined; // 报错 let { prop: y } = null; // 报错
function test([x, y]) { return x + y; } test([1, 2]);//3 参数传递到函数的那一刻被解构成x和y了
解构赋值虽然很方便,可是解析起来并不容易。对于编译器来讲,一个式子究竟是模式,仍是表达式,没有办法从一开始就知道,必须解析到(或解析不到)等号才能知道。
由此带来的问题是,若是模式中出现圆括号怎么处理。ES6 的规则是,只要有可能致使解构的歧义,就不得使用圆括号。
可是,这条规则实际上不那么容易辨别,处理起来至关麻烦。所以,建议只要有可能,就不要在模式中放置圆括号。
不能使用圆括号的状况
(1)变量声明语句
// 所有报错 let [(a)] = [1]; let {x: (c)} = {}; let ({x: c}) = {}; let {(x: c)} = {}; let {(x): c} = {}; let { o: ({ p: p }) } = { o: { p: 2 } };
(2)函数参数
// 报错 function f([(z)]) { return z; } // 报错 function f([z,(x)]) { return x; }
(3)赋值语句的模式
// 所有报错 ({ p: a }) = { p: 42 }; ([a]) = [5]; // 报错 [({ p: a }), { x: c }] = [{}, {}];
可使用圆括号的状况
可使用圆括号的状况只有一种:赋值语句的非模式部分,可使用圆括号。
[(b)] = [3]; // 正确 ({ p: (d) } = {}); // 正确 [(parseInt.prop)] = [3]; // 正确
(1)交换变量的值
let x = 1; let y = 2; [x, y] = [y, x];
(2)从函数返回多个值
function getParams() { return {name: 'Jason', id: 411, age: 24}; } let {name, id, age} = getParams();
(3)函数参数的定义
function test([x, y, z]) {} test([1, 2, 3]); function test({ x, y, z }) {} test({ x: 1, y: 2, z: 3 });
(4)提取 JSON 数据
const jsonData = { name: 'Jason', id: 411, age: 24 }; let {name, id, age: yearOld} = jsonData; console.log(name, id, yearOld);//Jason 411 24
(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
(7)输入模块的指定方法
import {mapState, mapGetters, mapMutations, mapActions, createNamespacedHelpers} from 'vuex';