ES6解构赋值

2018-01-15  本文已影响0人  不流泪的洋葱

ES6解构赋值

什么是解构赋值

MDN解释: 解构赋值语法是一个 Javascript 表达式,这使得可以将值从数组或属性从对象提取到不同的变量中。

从数组中提取值

  1. 变量与数组中的变量一一对应(包括类型):
  var [a,b,c] = [1,2,3];

  console.log(a); //1
  console.log(b); //2
  console.log(c); //3

解析嵌套对象:

解析粒度1

  var [one,two,three] = [1,{a:{n1:2,n2:3},b:4,c:5},6];

  console.log(one);   //1
  console.log(two);   //{a:{n1:2,n2:3},b:4,c:5}
  console.log(three); //6

解析粒度2

  var [one,{a,b,c}, three] = [1,{a:{n1:2,n2:3},b:4,c:5},6];

  console.log(one);   //1
  console.log(a);     //{2,3}
  console.log(b);     //4
  console.log(c);     //5
  console.log(three); //6

解析粒度3

  var [one,{a:{n1,n2},b,c}, three] = [1,{a:{n1:2,n2:3},b:4,c:5},6];

  console.log(one);   //1
  console.log(n1);    //2
  console.log(n2);    //3
  console.log(b);     //4
  console.log(c);     //5
  console.log(three); //6

解析嵌套数组:

解析粒度1

  var [one,two,three] = [1,[[2,3],4,5],6];

  console.log(one);   //1
  console.log(two);   //[[2,3],4,5]
  console.log(three); //6

解析粒度2

  var [one,[a,b,c],three] = [1,[[2,3],4,5],6];

  console.log(one);   //1
  console.log(a);     //[2,3]
  console.log(b);     //4
  console.log(c);     //5
  console.log(three); //6

解析粒度3

  var [one,[[n1,n2],b,c],three] = [1,[[2,3],4,5],6];

  console.log(one);   //1
  console.log(n1);    //2
  console.log(n2);    //3
  console.log(b);     //4
  console.log(c);     //5
  console.log(three); //6

上述例子说明了根据具体需可选择不同的解析粒度.

  1. 变量数量小于数组变量长度(不完全解析)
  var [a,b] = [1,2,3];

  console.log(a); //1
  console.log(b); //2
  1. 变量数量大于数组长度
  var [a,b,c] = [1,2];

  console.log(a); //1
  console.log(b); //2
  console.log(c); //undefined

使用解构赋值表达式时,如果指定的局部变量名称在数组或对象中不存在(解构不成功),那么这个局部变量会被赋值为undefined

  1. 为了防止从数组中取出一个变量为undefined的对象,可以对赋值表达式左边的变量设置默认值。
  var [a,b,c=3] = [1,2];

  console.log(a); //1
  console.log(b); //2
  console.log(c); //3

若能够解析到数组中的变量,赋值表达式左边变量默认值会被覆盖

  var [a,b,c=3] = [1,2,4];

  console.log(a); //1
  console.log(b); //2
  console.log(c); //4
  1. 在一个解构表达式中可以交换两个变量的值。
  var a = 1;
  var b = 3;
  var c;         //临时变量

  c = a;
  a = b;
  b = c;

  console.log(a); //3
  console.log(b); //1
  var a = 1;
  var b = 3;

  [a, b] = [b, a]
  console.log(a); //3
  console.log(b); //1
  1. 将剩余数组赋值给一个变量(剩余模式)
  var [a,b, ...c] = [1,2,3,4,5];

  console.log(a); //1
  console.log(b); //2
  console.log(c); //3,4,5
  console.log(Array.prototype.isPrototypeOf(c)) //true
  1. 忽略部分值
  var [a, ,b, ,c] = [1,2,3,4,5];

  console.log(a); //1
  console.log(b); //3
  console.log(c); //5
  1. 解构失败
  var [a,b,c] = [1,2];

  console.log(a); //1
  console.log(b); //2
  console.log(c); //undefined

解构以下类型会引发报错

var [a,b,c] = null; //null is not iterable
var [a,b,c] = undefined; //undefined is not iterable
var [a,b,c] = 0; //0 is not iterable
var [a,b,c] = NaN; //NaN is not iterable
var [a,b,c] = true; //true is not iterable
......

从对象中提取值

  1. 变量名与对象属性名称相同
  var {name,age} = {name:"Nicholas CZ",age:18};

  console.log(name); //Nicholas CZ
  console.log(age); //18

顺序可以不同

  var {age,name} = {name:"Nicholas CZ",age:18};

  console.log(name); //Nicholas CZ
  console.log(age); //18
  1. 解构嵌套对象
 var o = {
   person:{
     name:{
       firstName:"Nicholas",
       lastName:"CZ"
     },
     age:18
   },
   job:"JS Expert"
 }

  var {person, job} = o;

  console.log(person); //{name: {…}, age: 18}
  console.log(job); //18

当然像数组一样也可实现不同的解析粒度

 var o = {
   person:{
     name:{
       firstName:"Nicholas",
       lastName:"CZ"
     },
     age:18
   },
   job:"JS Expert"
 }

  var {person:{name:{firstName,lastName},age}, job} = o;

  console.log(firstName); //Nicholas
  console.log(lastName); //CZ
  console.log(age); //18
  console.log(job); //JS Expert

注意事项

上一篇下一篇

猜你喜欢

热点阅读