+ 1

Why the answer is NaN?

class enemy{ constructor(height,strength){ this.height=height; this.strength=strength;} } let chris=new enemy(100,50); let{prop1,prop2} = chris; console.log(prop1 + prop2)

28th Jan 2023, 2:54 PM
Mohammad Tausiful Alam
Mohammad Tausiful Alam - avatar
3 Respostas
+ 4
Use the actual names of the properties when object deconstructing let{height, strength} = chris; if you want/need to use an alias then you need both the original and the substitute names. let{height: prop1,strength: prop2} = chris;
28th Jan 2023, 5:03 PM
ODLNT
ODLNT - avatar
+ 6
prop1 and prop2 are undefined, hence you get NaN when trying to add them. Have you considered using let [prop1, prop2] = [chris.height, chris.strength]; ?
28th Jan 2023, 3:49 PM
Lisa
Lisa - avatar
+ 4
We'd probably need to see more than just that. Do you have all your code wrapped in a window.onload?
28th Jan 2023, 2:56 PM
Ausgrindtube
Ausgrindtube - avatar