Extends
Extends
π¨βπΌ Let's build a shape hierarchy. We'll create a base
Shape class and extend
it with specific shapes like Circle and Rectangle.π¨ Open
and:
- Create a
Shapebase class with:- Public field:
color(string) - Constructor
(color: string)
- Public field:
- Create a
Circleclass thatextends Shapewith:- Public field:
radius(number) - Constructor
(color, radius)that initializes the parent viasuper(color)
- Public field:
- Create a
Rectangleclass thatextends Shapewith:- Public fields:
width(number),height(number) - Constructor
(color, width, height)that callssuper(color)
- Public fields:
- Export all three:
export { Shape, Circle, Rectangle }
Fixtures and success criteria
const circle = new Circle('red', 5)
const rectangle = new Rectangle('blue', 10, 20)
circle.color === 'red'andcircle.radius === 5rectangle.color === 'blue',width === 10,height === 20circle instanceof Shapeandrectangle instanceof Shapeare bothtrue(each subclass is substitutable as aShape)