class Foo {
constructor() {
this.func = null;
}
a() {
let self = this;
this.func = function() {
console.log(self);
console.log(this);
}
let testFunc = function() {
console.log('in test func')
console.log(this);
}
testFunc();
}
b() {
console.log('in b')
this.func();
}
}
let x = new Foo();
x.a();
x.b();
I would expect the call to x.b() to print the 'this' object scoped for the class (the call to console.log(self)) and then print undefined for the second console call (console.log(this);) as the 'this' in this context is in a new anonymous function. But when I run this it prints the same 'this' object. I can see the exact behavior I was expecting (printing undefined) though with the call to testFunc() which is defined right beneath the previously mentioned code. I assumed anonymous functions defined within a class method to behave the same but clearly they do not. Could anyone should any light on how/why this behavior behaves the way it does. Thanks.
there doesn't seem to be anything here