ES6 语法 之 destructuring

ES6允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring)。

ES6写法

let cat = 'ken'
let dog = 'lili'
let zoo = {cat, dog}
console.log(zoo)  //Object {cat: "ken", dog: "lili"}  

反过来可以这样写

let dog = {type: 'animal', many: 2}
let { type, many} = dog
console.log(type, many)   //animal 2

其实这里我们写习惯了还是可以省很多代码量的,比如从链接里取参数,从react的this.state中取属性

posted @ 2017-07-11 15:31  H5工程师  阅读(222)  评论(0编辑  收藏  举报