Skip to content

this.comics is undefined #5

@deshmukhrajvardhan

Description

@deshmukhrajvardhan

I tried to output this.comics to the console but it said

TypeError: this.comics is undefined

So, i defined it in the constructor. Is there some other way of doing it?
I have attached the working code snippet below:

interface IMarvel {
  location: string;
  parentCompany: string;
  comics: string [];
  print() : string;
  boughtOut() : void;

}

class Marvel {
  location: string;
  parentCompany: string;
  comics: string [];

  constructor(location: string, parentCompany: string,comics:string) {
    this.location = location;
      this.parentCompany = parentCompany;
      //this.comics = [comics];
  }

  print() : string {
    return `Marvel is located in ${this.location}, and is owned by ${this.parentCompany}`;
  }

  boughtOut(newParent : string) : void {
    this.parentCompany = newParent;
  }

  madeProfit(profit?: number) : string {
    if (profit) {
      return 'Yes';
    } else {
      return 'No';
    }
  }

  addNewComics(...comics: string []) : void{
    this.comics = this.comics.concat(comics);
  }
  
  modifySuperhero(name: string, callback: (name: string) => string) : void {
    callback(name);
    }

    printVars(): void{
      console.log(this.comics,this.location,this.parentCompany)
  }
}

let comicObj = new Marvel("Hawaii", "H-Corp.","GuyFox");
comicObj.addNewComics("SpiderMan", "X-men")
comicObj.printVars()

Also, what is the purpose of the interface? As, it isn't used in this example.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions