class Simulator {
constructor() {
this.gates = Array();
this.gates.push(new AndGate(200, 200));
}
initialize() {
let canvas = document.getElementById('board');
canvas.width = 800;
canvas.height = 500;
canvas.setAttribute("style", "border: 1px solid black");
this.gates.push(new AndGate(100, 100));
}
run() {
setTimeout(this.onLoop, 1000);
}
onLoop() {
for (let gate of this.gates) {
gate.render();
}
}
}
let sim = new Simulator();
sim.initialize();
sim.run();
Run Code Online (Sandbox Code Playgroud)
出于某种原因,我的 TypeScript 类的 JS 转译版本在onLoop函数中引发错误。它报告TypeError: this.gates is undefined。但是,如果我访问sim(一个 Simulator 对象)并手动访问它定义的 gates 属性。我可以从控制台手动运行 onLoop 代码。