In the class below, I use this 9 times to deal with member variables and functions. It's clogging up all my beautiful code! Is there anything that I can do to make it prettier? For example, is there anyway to access the context member variable without referring to this?
//controls timing and game phases.
import { Context } from "./helpers/Context"
import { Model } from "./Model"
import { View } from "./View"
export class Controller {
context : Context;
constructor(context : Context) {
this.context = context;
}
//make other objects in context, add them in.
begin = () : void => {
this.context.model = new Model(this.context);
this.context.view = new View(this.context);
this.init();
}
init = () : void => {
this.context.model.init();
this.context.view.init();
//causes an error! help.
this.context.model.preloadModels([
"/models/bleachers.obj"
], () => { this.buildWorld(); })
}
buildWorld = () : void => {
this.context.view.makeGrass();
this.context.view.makeSkybox();
this.context.view.makeBleachersOnEdgeOfField();
}
}
If you're used to languages like C or Java where you don't have to use this for class fields, it can look weird at first. I thought the same thing too. But for languages like Javascript and Python, it's normal to write this and self a lot of times. I think it's just a language-specific style thing that can look ugly to someone who is not used to seeing it, but this is the normal way and most JS programmers would not think it's ugly because they are used to it.