Here is the scenario that caught my attention:
abstract class Base {
public _obj = { name: 'Test' }
print1() {
console.log(this._obj)
}
print2() {
console.log(this)
}
}
class Child extends Base {
print2() { // overriding
// do some stuff
console.log(this._obj)
}
}
function test(cb: any) {
cb()
}
const obj = new Child()
obj.print1() // works fine!
obj.print2() // works fine!
test(obj.print1) // returns undefined
When printing this
, it shows up as undefined.
If I pass the function as a parameter, why does the context of this
get lost?
Appreciate your insights.