Home > database >  class square instance of rectangle how to apply
class square instance of rectangle how to apply

Time:11-13

I have a class for a general Shape, my arguments would be 'name', 'sides' and 'length, and then I have a Square class which is an instance of Shape. it is supposed that a square has 4 equals sides, so I am trying to define in my Square class a constructor with the only argument of length, because a rectangle with the same length in the four sides is a Square.

do I already have 2 predefined values, name = square and sides = 4; the only value I have to input for my Square class is my sideLength but for some reason I can't figure out is logging 'undefined;

the prototype of Square would be shape, and is functioning well in creation of objects, however Square class is not getting sidelenght value, although object is being created, with default values (name and sides) but cannot use method calcArea() for lack of sidelength value;

class Shape {

name;
sides;
sideLength;

constructor(_name, _sides, _sideLength){
  this.name = _name;
  this.sides = _sides;
  this.sideLength = _sideLength;
}

calcArea(){
  console.log(this.sides*this.sideLength);
}    

}

class Square extends Shape {

constructor(_sideLength) {
    super(_sideLength);
    this.name = 'square';
    this.sides = 4;    
    }    
}

const newShape = new Shape('pentagon', 5, 6);
const newSquare = new Square(5);

CodePudding user response:

You should pass all the parameters to the super constructor so that it can populate the instance with the needed properties, including the sideLength. The signature is

constructor(_name, _sides, _sideLength){

so your super call should include those 3 parameters. (Passing it just super(_sideLength); results in that being the name, which isn't what you want.)

class Shape {
  constructor(_name, _sides, _sideLength) {
    this.name = _name;
    this.sides = _sides;
    this.sideLength = _sideLength;
  }
  calcArea() {
    console.log(this.sides * this.sideLength);
  }
}

class Square extends Shape {
  constructor(_sideLength) {
    super('square', 4, _sideLength);
  }
}

const newShape = new Shape('pentagon', 5, 6);
const newSquare = new Square(5);
newSquare.calcArea();

CodePudding user response:

Your Shape constructor takes 3 arguments _name, _sides, _sideLength, but you pass only one _sideLength so it's assigned to name

If you wanted to skip a value you have to pass undefined:

constructor(_sideLength) {
    super(undefined, undefined, _sideLength);
    this.name = 'square';
    this.sides = 4;    
    }    
}

class Shape {
  name;
  sides;
  sideLength;

  constructor(_name, _sides, _sideLength) {
    this.name = _name;
    this.sides = _sides;
    this.sideLength = _sideLength;
  }

  calcArea() {
    console.log(this.sides * this.sideLength);
  }

}

class Square extends Shape {

  constructor(_sideLength) {
    super(undefined, undefined, _sideLength);
    this.name = 'square';
    this.sides = 4;
  }
}

const newShape = new Shape('pentagon', 5, 6);
const newSquare = new Square(5);
console.log(newSquare.sideLength)

  • Related