开发者

JavaScript global scope

开发者 https://www.devze.com 2023-02-18 09:25 出处:网络
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.

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

精彩评论

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

关注公众号