I'm still getting the hang of typescript, and I've come across a situation where a function inside a Class constructor is calling another function, but when trying to access this
within sayHelloAgain(), it returns the window object instead.
Within Greeter.init()
, I am using
this.sayHello("message string", parameterCallback)
class Greeter {
init() {
this.sayHello("hello", this.sayHelloAgain);
}
sayHello(msg, callbackFunction) {
// Returns the Greeter object
console.log(this);
callbackFunction(msg);
}
sayHelloAgain(msg) {
// Instead of returning the Greeter object, it returns the Window object
console.log(this)
}
}
let greeter = new Greeter("world");