どのように私は以下を達成することができますか(このは望ましい結果を達成しません)。 "this.AddComponent(new MoveComponent(0、0、this));"という行を参照しています。これにクラスShape
のメソッドAddComponent
の実装にECMAScript 6 - クラス内から「self」への参照を渡すにはどうすればよいですか?
// shape.js
export default class Shape {
constructor(name) {
this.name = name;
this.Components = [];
}
AddComponent(component) {
this.Components.push(component);
}
}
// component.js
export default class Component {
constructor(name) {
this.name = name;
}
}
// square.js
import Shape from "./shape";
import MoveComponent from "./moveComponent";
export default class Square extends Shape {
constructor() {
super("square");
this.AddComponent(new MoveComponent(0, 0, this));
}
}
// moveComponent.js
import Component from "./component";
export default class MoveComponent extends Component {
constructor(x, y, parentShape) {
super("movement");
this.x = x;
this.y = y;
this.ParentShape = parentShape;
}
}
あなたに具体的にどのようなエラーが実行されている "広場" クラスの? 「これは私が期待しているものです。 – loganfsmyth