在下面的类中,我使用this 9次来处理成员变量和函数。它堵塞了我所有美丽的代码!我能做些什么让它更漂亮吗?例如,是否仍然可以访问context成员变量而不引用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();
}
}发布于 2021-09-15 17:53:16
如果您已经习惯了像C或Java这样的语言,在这些语言中,您不必在类字段中使用this,那么它一开始可能看起来很奇怪。我也这么想。但是对于像Javascript和Python这样的语言来说,编写this和self是很正常的。我认为这只是一种特定于语言的风格,在那些不习惯看它的人看来很难看,但这是正常的方式,大多数JS程序员不会认为它很难看,因为他们已经习惯了。
https://stackoverflow.com/questions/69197631
复制相似问题