When trying to execute the following snippet, I expected the doFoo.call(obj, obj.foo)
call to return "inside obj"
as the dynamic scope of the previous element in the call stack has a reference to a
but the call has the global context instead. What am I missing here?
function foo() {
// debugger
console.log(this, this.a);
}
function doFoo(fn) {
// `fn` is just another reference to `foo`
// debugger
fn()
// <-- call-site!
}
var obj = {
a: 'inside obj',
foo: foo
};
var a = "oops, global"; // `a` also property on global object
doFoo(obj.foo); // "oops, global"
doFoo.call(obj, obj.foo) // "oops, global"```