Home > database >  How do you log information using a static method from a class using Typescript?
How do you log information using a static method from a class using Typescript?

Time:02-07

This is for a school project. I'm having trouble getting the names from the class to log using a method. I have a class that maakes the monster and another one that extends it.

abstract class genMonster {
  constructor(
    public id: string,
    public name: string,
    public weaknesses: string[],
    public location: string,
    public challenge: number,
    public mortality: boolean = false,
    public safety: number,
  ) {}

  monsterLogger() {
    return this
  }
}

class ghost extends genMonster {
  constructor(id: string, name: string, weaknesses: string[], location: string, challenge: number, mortality: boolean, safety: number, public type: string, public signs: string[]) {
    super(id, name, weaknesses, location, challenge, mortality, safety);
  }

  get info() {
    return console.log(this.name, this.type, this.signs);
  }
}

I have the monster objects

const Jerry = new ghost("0234", "Jerry", ["soap", "attractive people"], "-74.4835, 171.4803",  3, false, 2, "boring", ["libra", "ectoplasm puddles", "yamaha piano music"]);
const Patricia = new ghost("8765", "Patricia", ["being sent to the corner", "milk and cookies"], "-89.3921, -30.4079",  1, false, 2, "spooky", ["stray drawing supplies", "messy living rooms", "spilled milk"]);
const Lola = new ghost("4569", "Lola", ["makeup remover", "tied shoelaces"], "-61.6134, -90.1008",  10, false, 10, "boss music", ["water tasting like cotton candy", "spontaneous laughing", "socks mysteriously becoming fun and colorful"]);

and this is the method I tried but doesn't work

class organize extends genMonster{
  constructor(id: string, name: string, weaknesses: string[], location: string, challenge: number, mortality: boolean, safety: number){
    super(id, name, weaknesses, location, challenge, mortality, safety)
  }

  public static getNames(){
    return this.name
  }
}

console.log(organize.getNames());
//logs 'organize'

Any help is appreciated

CodePudding user response:

The getNames of organize is static and returns the constructor name itself ("organize"). Do you intend to get the name of the ghost instances themselves (Jerry, Patricia, Lola)? If so...

console.log(Jerry.name, Patricia.name, Lola.name);

CodePudding user response:

Since a static method cannot access instance fields, you might need to keep a record of all the names taken by monsters on a static field (takenNames) when each monster is created.

Then you could have a static method (getTakenNames) that returns takenNames

abstract class genMonster {
  static takenNames: string[] = [];
  static getTakenNames(): string[] {
    return ghost.takenNames;
  }
  constructor(
    public id: string,
    public name: string,
    public weaknesses: string[],
    public location: string,
    public challenge: number,
    public mortality: boolean = false,
    public safety: number,
  ) {}

  monsterLogger() {
    return this
  }
}

class ghost extends genMonster {


  constructor(id: string, name: string, weaknesses: string[], location: string, challenge: number, mortality: boolean, safety: number, public type: string, public signs: string[]) {
    super(id, name, weaknesses, location, challenge, mortality, safety);
    ghost.takenNames.push(name);
  }

  get info() {
    return console.log(this.name, this.type, this.signs);
  }
}

const Jerry = new ghost("0234", "Jerry", ["soap", "attractive people"], "-74.4835, 171.4803",  3, false, 2, "boring", ["libra", "ectoplasm puddles", "yamaha piano music"]);
const Patricia = new ghost("8765", "Patricia", ["being sent to the corner", "milk and cookies"], "-89.3921, -30.4079",  1, false, 2, "spooky", ["stray drawing supplies", "messy living rooms", "spilled milk"]);
const Lola = new ghost("4569", "Lola", ["makeup remover", "tied shoelaces"], "-61.6134, -90.1008",  10, false, 10, "boss music", ["water tasting like cotton candy", "spontaneous laughing", "socks mysteriously becoming fun and colorful"]);

console.log(genMonster.getTakenNames())

playground

  •  Tags:  
  • Related