JavaScript global scope
I ran this code in my console but got undefined. However I expected it to return 1
because function 2 returns a, which is a var in the global scope.
Can you please explain where I'm mistaken? thank you.
var a = 1;
function f1() {var a = 1; f2();}
function f2() {return a开发者_JAVA技巧;}
f1();
You aren't doing anything with the return value of f2
. You'd need to do this:
var a = 1;
function f1() {var a = 1; return f2();} // NB pass the return value on
function f2() {return a;}
f1();
精彩评论