We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
/* * Write code that adds an 'area' method to the Rectangle class' prototype */Rectangle.prototype.area=function(){return(this.w*this.h);}/* * Create a Square class that inherits from Rectangle and implement its class constructor */classSquareextendsRectangle{constructor(s){this.s=s;}area(){return(this.s*this.s);}}
Day 5: Inheritance
You are viewing a single comment's thread. Return to all comments →
what's wrong with my code??
You should call the constructor of the parent class from the child class, i.e., the Square class constructor should call
super(s, s)
.You can check the editorial.
I noticed utilizing super() and super(s) can pass the test case, too. Why is that? Thank you.
super() initializes its parent constructor. In this case it calls constructor of Rectangle. So using super(s,s) will initialize w and h.
However if you do not provide proper argument to super then you have to manually assign the value of w and h. Like: