+ 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)
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;
+ 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];
?
+ 4
We'd probably need to see more than just that.
Do you have all your code wrapped in a window.onload?