当使用一个特定的变量名时,对象重构会给我一个NaN值。

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

我的对象有2个属性,当这些属性被命名为左和顶时 { left: rect.left, top: rect.top}.破坏对象后,我的x和y变量都是NaN。

const { x, y } = this.getCanvasPosition(this.canvasHex.current);

但如果我把该对象的属性重命名为 x 和 y { x: rect.left, y: rect.top},我得到了我想要的值。

我想知道这里到底发生了什么。

javascript object destructuring
1个回答
3
投票

你需要对属性进行重命名,因为你没有属性。xylefttop.

const { left: x, top: y } = this.getCanvasPosition(this.canvasHex.current);

const { left: x, top: y } = { left: 10, top: 5 };

console.log(x, y);
© www.soinside.com 2019 - 2024. All rights reserved.