New course! Every coder should learn Generative AI!
Try a free lesson0
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);
3 Answers
+ 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);
+ 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;
}
}
+ 1
Thanks both of you friends.. :)