traceur を使用して ES6 でクラスをテストしていますが、期待どおりに動作しません。
メソッドを別のクラスの参照として使用しようとしていますが、呼び出されると、の値を読み取るときに呼び出し元クラスの参照を取得しますthis
。
これが私のコードです:
class A {
constructor(anotherMethod){
this.anotherMethod = anotherMethod;
this.name = "A";
}
myMethod (){
console.log(this.name);
this.anotherMethod();
}
}
class B {
constructor(){
this.a = new A(this.myMethod);
this.name = "B";
}
myMethod(){
console.log(this.name);
}
}
var c = new B();
c.a.myMethod();
私の予想されるログは次のとおりです。
A
B
しかし、それは示しています:
A
A