先上代码:
var scope = 'global scope';function checkscope(){ var scope = 'local scope'; function f(){return scope;} return f(); } checkscope();
输出:local scope
如果把这段代码改一下,把里面的括号移动到外面:
var scope = 'global scope';function checkscope(){ var scope = 'local scope'; function f(){return scope;} return f; } checkscope()();
输出:local scope
我的问题:
1.这段代码到底是要干什么?
2.为什么代码会返回local scope而不是global scope?
3.为什么把f后面的()移动到外面还是返回一样的值?f没了括号,return f是什么意思呢?
4.js里面是不是只要是var申明的都是局部变量,没有var的都是全局变量?
5.最后一个问题,我看了网上的不少闭包的文章,但是还是对于闭包这个概念不是太懂,只知道闭包就是里面的函数可以访问外面的函数的成员变量,但是反过来不行,有哪位大神可以举几个通俗易懂的例子帮我理解下到底什么事闭包吗?
谢谢各位啦!:D
跃然一笑
相关分类