是否可以进行解构并将其直接设置为变量?

问题描述 投票:0回答:1

我正在尝试分解对象,但是那些变量已经存在,像这样

const x,y
const {x,y} = complexPoint
const point = {x,y}

是否存在没有重命名解构变量的方法?像这样和更新点避免const定义?

const point = {x,y} = complexPoint
javascript ecmascript-6 destructuring object-destructuring
1个回答
0
投票

您可以使用闭包将结构分解为现有变量:

const complexPoint = { x: 1, y: 2, z: 3 }
let x,y
({x,y} = complexPoint)
const point = {x,y}

console.log(point)
© www.soinside.com 2019 - 2024. All rights reserved.