-
Notifications
You must be signed in to change notification settings - Fork 9
Open
Description
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
Labels
No labels