Instantiating an object of one class within the constructor of another class

Asked

Viewed 120 times

0

[SOLVED]How do I prompt the Mars object within the Rover class? For, is returning me the following error: "Mars is not defined"

class Rover{

  constructor(orderedX,orderedY,orientation,toMove){
    this.orderedX = orderedX;
    this.orderedY = orderedY;
    this.orientation = orientation;
    this.toMove = toMove;
    this.marsZise = new Mars(5,5);
    this.running();

  }

class Mars{
  constructor(sizeX,sizeY){
    this._orderedlengthX = sizeX;
    this._orderedlengthY = sizeY;

  }
  • Please redirect your Question for stackoverflow. Thanks :)

  • you’re using typescript? if yes, just do: Rover extends Mars

1 answer

0


From what I’ve noticed, you’re using typescript, to inherit the classe, just use the extends as follows:

Rover extends Mars
  • I decided here, I didn’t really want to inherit, I was wrong to pass the properties of the Rover object by parameter on Mars :( (Wrong logic there) I just wanted to instantiate the Mars object inside the Rover constructor, then it’s : this.marsSize = new Mars(5,5) - I changed the logic in the Mars-class Thank you

  • 1

    Using Ecmascript6

  • @ttlelis_12 this there :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.