简体   繁体   中英

Object destructuring gives me a NaN value when using a specific variable name

My object has 2 properties, when those properties are named left and top { left: rect.left, top: rect.top} . After destructuring the object, my x and y variables are both NaN.

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

But if I rename my properties of that object to x and y { x: rect.left, y: rect.top} , I get the values I'm looking for.

I'm wondering what is exactly going on here.

You need a renaming of the properties, because you have no properties x and y , but left and top .

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

 const { left: x, top: y } = { left: 10, top: 5 }; console.log(x, y);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM