+ 2

Js classes problem

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) Why this outputs NaN? Shouldnt it output 150?

11th Feb 2021, 3:25 PM
David
David - avatar
2 Respuestas
+ 15
class enemy { constructor(height, strength) { this.height = height; this.strength = strength; } } let chris = new enemy(100, 50); let {height, strength} = chris; console.log(height+strength); => 150 or, class enemy { constructor(height, strength) { this.height = height; this.strength = strength; } } let chris = new enemy(100, 50); let {height:prop1, strength:prop2} = chris; console.log(prop1+prop2); => 150 prop1 and prop2 are not properties of object so they both are undefined and adding prop1 and prop2 results NaN
11th Feb 2021, 3:47 PM
JOY
JOY - avatar
+ 6
if you want to store height in prop1 and strength in prop2 by destructuring you should do: let {height:prop1,strength:prop2} = chris; console.log(prop1+prop2); // 150
11th Feb 2021, 4:00 PM
visph
visph - avatar