使用特定变量名称时,对象分解给我一个NaN值

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

我的对象具有2个属性,当这些属性分别命名为left和top { 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
2个回答
0
投票

需要

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

-1
投票

您需要重命名属性,因为您没有属性xy,但没有lefttop

const { left: x, top: y } = this.getCanvasPosition(this.canvasHex.current);
© www.soinside.com 2019 - 2024. All rights reserved.