Sort by

recency

|

126 Discussions

|

  • + 0 comments
    /*
     *  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
     */
    class Square extends Rectangle {
      constructor(side) {
        super(side, side);
      }
    }
    
  • + 0 comments
    Rectangle.prototype.area = function () {
      return this.w * this.h;
    };
    
    class Square extends Rectangle {
     constructor(side) {
        super(side,side);
      }
    }
    
  • + 0 comments

    If we dont use second parameter in Square, there is no need to overload parent's constructor:

    class Square extends Rectangle{
        area = function(){
            return this.w * this.w
        }
    }
    

    But I presume such magic works only in JavaScript :)

  • + 0 comments

    wanted to know can't we override the prototype methods like earlier I was trying to create area method in square class too and from there was eventually calling super.area() so as to idirectly call reactangle's area method. Can someone please help me with any conceptual error which I am doing here?

  • + 0 comments
    Rectangle.prototype.area=function(){
        return this.w*this.h;
    }
    
    class Square extends Rectangle{
    //The super() method refers to the parent class.
    // By calling the super() method in the constructor method, we call the parent's constructor method and gets access to the parent's properties and methods.
        constructor(width){
            super(width,width);
            this.width=width;
        }
    }