class MyClass {
propOne = 'prop 1'
doSomething() {
console.log('executing doSomething')
console.log(this.propOne)
}
example1() {
setTimeout(() => { this.doSomething() }, 3000)
}
example2() {
setTimeout(this.doSomething, 3000)
}
}
const obj = new MyClass()
obj.example1()
// LOG: VM1240:6 executing doSomething
// LOG: VM1240:7 prop 1
obj.example2()
// LOG: VM1240:6 executing doSomething
// VM1240:7 undefined
Why obj.example2() prints prop1 undefined?
I know that an arrow function does not have a 'this' value of its own, anyway this does not seem the specific case of this behavior.