开发者

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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜