对象解构
基本用法
1 2 3 4 5 6 7 8 9
| let node = { type: "Indentifier", name: "foo" }
let { type, name } = node
console.log(type) console.log(type)
|
解构赋值
1 2 3 4 5 6 7 8 9 10 11
| let node = { type: "Indentifier", name: "foo" } let type = 'Literal' let name = 5
({ type, name } = node)
console.log(type) console.log(name)
|
** 注: ** 一定要用小括号包裹解构赋值语句,JavaScript引擎将一对开放的花括号视为一个代码块,而语法规定,代码块语句不允许出现在解构赋值语句左侧,添加小括号后可以将块语句转化为一个表达式,从而实现整个解构赋值的过程。
给函数解构赋值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| let node = { type: "Indentifier", name: "foo" } let type = 'Literal' let name = 5
function outputInfo(value) { console.log(value === node) }
outputInfo({ type, name } = node)
console.log(type) console.log(name)
|
默认值
1 2 3 4 5 6 7 8 9 10 11 12
| let node = { type: "Indentifier", name: "foo" } let type = 'Literal' let name = 5
let { type, name, value } = node
console.log(type) console.log(name) console.log(value)
|
1 2 3 4 5 6 7 8 9 10 11 12
| let node = { type: "Indentifier", name: "foo" } let type = 'Literal' let name = 5
let { type, name, value = true } = node
console.log(type) console.log(name) console.log(value)
|
非同名局部变量赋值
1 2 3 4 5 6 7 8 9
| let node = { type: "Indentifier", name: "foo" }
let { type: localType, name: localName } = node
console.log(localType) console.log(localName)
|
默认值
1 2 3 4 5 6 7 8
| let node = { type: "Indentifier" }
let { type: localType, name: localName = "bar" } = node
console.log(localType) console.log(localName)
|
嵌套对象解构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| let node = { type: "Indentifier", name: "foo", loc: { start: { line: 1, column: 1 }, end: { line: 1, column: 4 } } }
let { loc: { start } } = node
console.log(start.line) console.log(start.column)
|
非同名局部变量赋值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| let node = { type: "Indentifier", name: "foo", loc: { start: { line: 1, column: 1 }, end: { line: 1, column: 4 } } }
let { loc: { start: localStart } } = node
console.log(localStart.line) console.log(localStart.column)
|
数组解构
基本用法
1 2 3 4 5 6
| let colors = [ "red", "green", "blue" ]
let [ firstColor, secondColor ] = colors
console.log(firstColor) console.log(secondColor)
|
取特定位置
1 2 3 4 5
| let colors = [ "red", "green", "blue" ]
let [ , , thirdColor ] = colors
console.log(thirdColor)
|
解构赋值
1 2 3 4 5 6 7 8
| let colors = [ "red", "green", "blue" ] let firstColor = 'black' let secondColor = 'purple'
let [ firstColor, secondColor ] = colors
console.log(firstColor) console.log(secondColor)
|
交换变量
ES5
1 2 3 4 5 6 7
| let a = 1 let b = 2 let tmp
tmp = a a = b b = tmp
|
ES6
1 2 3 4
| let a = 1 let b = 2
[ a, b ] = [ b, a ]
|
默认值
1 2 3 4 5 6
| let colors = [ "red" ]
let [ firstColor, secondColor = 'green' ] = colors
console.log(firstColor) console.log(secondColor)
|
嵌套数组解构
1 2 3 4 5 6
| let colors = [ "red", [ "green", "lightgreen" ], "blue" ]
let [ firstColor, [ secondColor ] ] = colors
console.log(firstColor) console.log(secondColor)
|
不定元素
1 2 3 4 5 6
| let colors = [ "red", "green", "blue" ]
let [ firstColor, ...restColors ] = colors
console.log(firstColor) console.log(restColors)
|
克隆
1 2 3 4 5
| let colors = [ "red", "green", "blue" ]
let [ ...cloneColors ] = colors
console.log(cloneColors)
|
混合解构
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| let node = { type: 'Identifier', name: 'foo', loc: { start: { line: 1, column: 1 }, end: { line: 1, column: 4 }, range: [0, 3] } }
let { loc: { start }, range: [startIndex] } = node
console.log(start.line) console.log(start.column) console.logs(startIndex)
|
参考
- 深入理解ES6 – 作者: 【美】Nicholas C. Zakas