0

How to implement Shape class above Circle ? I mean Circle and Rectangle class should be inherited from Shape.

I will be happy if someone give real code :)

here i made Circle class with prototype definition as well.

function Circle(radius){
 this.radius = radius;
 Circle.prototype.area = function(){return (Math.PI)* (Math.pow(this.radius,2));};
 }

 var circle1 = new Circle(5);
 circle1.radius; //5
 circle1.area() //78.53
P K
  • 9,972
  • 12
  • 53
  • 99

1 Answers1

2

You can use prototype to implement inheritance in JS for ex :

ChildClassName.prototype = new ParentClass();

In your case define shape class then extend it like this :

Circle.prototype = new Shape();

This will give you more info about the same :- http://phrogz.net/js/classes/OOPinJS2.html

Ved
  • 8,577
  • 7
  • 36
  • 69