I am reading this chapter in You don't know JS .
function baz() {
// call-stack is: `baz`
// so, our call-site is in the global scope
console.log("baz");
bar(); // <-- call-site for `bar`
}
function bar() {
// call-stack is: `baz` -> `bar`
// so, our call-site is in `baz`
console.log("bar");
foo(); // <-- call-site for `foo`
}
function foo() {
// call-stack is: `baz` -> `bar` -> `foo`
// so, our call-site is in `bar`
console.log("foo");
console.log(this);
}
baz(); // <-- call-site for `baz`
I was expecting the console.log(this)
in function foo
to print bar
, since bar
is the call site, but instead it seems to be window
.
How is the this
reference window
instead of bar
inside the function foo ?