When I run this code in node, the function foo prints undefined
. But if I run the same code in the Chrome or Firefox console I get the right output (oops, global
). Can anyone clarify why?
function foo() {
console.log(this.a);
}
function doFoo(fn) {
// `fn` is just another reference to `foo`
fn(); // <-- call-site!
}
var obj = {
a: 2,
foo: foo
};
var a = "oops, global"; // `a` also property on global object
doFoo(obj.foo); // "oops, global"