// 字符串可以化成数组,数组又是特殊的对象 str => [] => {}
let str = "Hello ES6";
let {0: a} = str; // 对象解构是根据属性
console.log(a); // H
let [b] = str; // 数组解构是根据位置
console.log(b); // H
let {length: len} = str; // 因为str可以转成数组,而数组都有length属性,故可以解构
console.log(len); // 9
// 注:解构赋值的规则是,只要等号右边的值不是对象或数组,就先将其转为对象。
let {toString: x} = len;
console.log(Number.prototype.toString) // true
let {toString: x} = (len === str.length);
console.log(x === Boolean.prototype.toString); // true
// 注: 由于undefined和null无法转为对象,所以对它们进行解构赋值,都会报错。
// let {x} = null; // TypeError
// let {y} = undefined; // TypeError
想要更加详细的学习ES6语法,推荐网站: http://es6.ruanyifeng.com/