In my code I'm creating a new anonymous class that is implementing an interface.
private service: CommandService;
this.command = new class implements Command {
execute(id: string): Promise<Result> {
const resultId: string = await this.service.getResultId();
return await this.service.getResult(resultId);
};
};
What would be the best way to access the service in this case? I'm thinking of either
- define
const _this = this
above the class. - give the service to the
execute
function.
Is there a better way to achieve this?
Edit:
I know that there's a wonderful answer here already, however, it doesn't explain how to access this
within an anonymous class.