解构赋值

解构赋值

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

1 数组

let [a, b, c] = [1, 2, 3];

从数组中提取值,按照对应位置,对变量赋值

2 对象

let { foo, bar } = { foo: "aaa", bar: "bbb" };
foo // "aaa"
bar // "bbb"

另外:解构赋值时,还可以设置默认值。

let [x, y = 'b'] = ['a']; // x='a', y='b'

 3 其他常用

let {getDetail , reDown} = this.props
//之后使用getDetail 和 reDown相当于如下
getDetail = this.props.getDetail 
reDown = this.props.reDown
原文地址:https://www.cnblogs.com/piaobodewu/p/9741985.html