I'm confused by what happens with this in this simple code:
function foo() {
alert(this.a );
}
function doFoo(fn) {
a = "local";
fn();
}
var a = "global";
doFoo( foo );
From what I've learned, deFoo is the context object from which foo is called so the alert's message should be "local". That works, unless I declare a inside doFoo:
function doFoo(fn) {
var a = "local";
fn();
}
Now the alert message is "global". Can someone explain this to me? I'm new to Javascript.