对象析构:
function getStock() {
return {
code: "IBM",
price: {
price1: 100,
price2: 200
},
aaa: 'xixi',
bbb: 'haha'
}
}
let { code: xcode, price: { price2 } } = getStock()
console.log(xcode, price2)
// “IBM” 200
数组析构:
let array1 = [1, 2, 3, 4]
let [number1, , , number4] = array1
console.log(number1) // 1
console.log(number4) // 4
let [num1, ...others] = array1
console.log(others) // [2, 3, 4]
function doSomething([num1, num2, ...others]) {
console.log(num1) // 1
console.log(num2) // 2
console.log(others) // [3, 4]
}
doSomething(array1)