I have these two classes
class Foo {
name = 'Foo';
foo() {
const bar = new Bar(this.onSomething);
bar.bar();
}
onSomething() {
console.log(this); //<= this where thing goes weird
}
}
class Bar {
name = 'Bar';
constructor(onSomethingCallback) {
this.onSomethingCallback = onSomethingCallback;
}
bar() {
this.onSomethingCallback();
}
}
const a = new Foo();
a.foo();
when the method onSomething
called , this
refer to the instance of Bar
class instead of Foo
class.
I expect this
to refer to instance of Foo
since the method onSomething
is in Foo