Sololearners plz help me to solve? Why this is showing 'undefined' as result? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Sololearners plz help me to solve? Why this is showing 'undefined' as result?

class Circle { pie(height, width) { this.height = height; this.width = width; } } const diagram = new Circle(5, 7); const angle = new Circle(8, 2); const wavy = new Circle(1, 3); console.log(diagram.height); console.log(angle.width); alert(wavy.height);

24th Jan 2023, 5:40 AM
Sony
Sony - avatar
3 Antworten
+ 3
When the new Cicle(5, 7) syntax is used, it doesnt know what to do with the arguments. When you try to access height and width properties on the instances created,, it will return undefined, because they are not set. So call the method "pie()" to create new instances. const diagram = new Circle(); diagram.pie(5, 7); const angle = new Circle(); angle.pie(8, 2); const wavy = new Circle(); wavy.pie(1, 3);
24th Jan 2023, 5:55 AM
Liamdev
Liamdev - avatar
+ 3
What is "pie" in your code? You should use the correct syntax to define a constructor: class Circle { constructor(height, width) { this.height = height; this.width = width; } }
24th Jan 2023, 5:51 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Thanks both of you friends.. :)
24th Jan 2023, 6:38 AM
Sony
Sony - avatar