2017-01-27 5 views
2

どのように私は以下を達成することができますか(このは望ましい結果を達成しません)。 "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; 
    } 
} 
+0

あなたに具体的にどのようなエラーが実行されている "広場" クラスの? 「これは私が期待しているものです。 – loganfsmyth

答えて

0

変更:

AddComponent(component) { 
    this.Components.push(component); 
} 

"use strict" 
 

 
class Shape { 
 
    constructor(name) { 
 
    this.name = name; 
 
    this.Components = []; 
 
    } 
 

 
    AddComponent(component) { 
 
    this.Components.push(component); 
 
    console.log(this) 
 
    } 
 
} 
 

 
class Component { 
 
    constructor(name) { 
 
    this.name = name; 
 
    } 
 
} 
 

 
class Square extends Shape { 
 
    constructor() { 
 
    super("square"); 
 
    this.AddComponent(new MoveComponent(0, 0, this)); 
 
    } 
 
} 
 

 
class MoveComponent extends Component { 
 
    constructor(x, y, parentShape) { 
 
    super("movement"); 
 
    this.x = x; 
 
    this.y = y; 
 
    this.ParentShape = parentShape; 
 
    } 
 
} 
 

 
new Square() // logs passed arguments

+0

これによってどのような問題が解決されますか? –

+0

私はばかです...ありがとうございます。 – mernstacking

+0

@torazaburo - 元のコードは、 'Components'が定義されていないというエラーをスローします。そのソースは、メソッド宣言の中に 'AddComponent(component){ components.push(component);}のように見えました。 } ' –

関連する問題