Why does the following snippet return true
and undefined
?
var fns = {
foo() {
console.log(this === window); // true (instead of false)
console.log(typeof this.bar); // undefined (instead of function)
},
bar() {},
};
Promise.resolve().then(fns.foo);
(If I run fns.foo();
instead of Promise.resolve().then(fns.foo);
it prints false
and function
as expected)
Is it a promise specific definition?
I checked
- MDN - this but found no reason for this behavior.
- the specification but did not understand it.