本帖最后由 WildGhost 于 2013-11-08 11:18:09 编辑

解决方案 »

  1.   

    不是簡單點說 generateClosure 執行過程中,get被執行,雖然它引用了 generateClosure 中的 count,但在 generateClosure 執行完之前, get也己經執行完了,所以整個過程運行完以后,所有局部變量都沒有被當前還存在的其它變量(對象)引用,己經被系統銷毀了。
      

  2.   

    《Professional Javascript for Web Developers》定义的很清晰啊
      

  3.   

    这个不算闭包。
    闭包比较关键的地方在于函数A执行完毕后,函数里的变量或参数并没有被回收而被其他函数B(常见的情况就是B在A内声明或定义)引用着。你的例子,
    第一个:generateClosure执行完后,它返回的函数(也就是counter)还引用着它的变量count,所以是闭包。
    第二个:generateClosure执行完后,它没有返回函数,它的外面也没有其他函数引用着它的变量,它的变量被回收,所以不是闭包。
      

  4.   

    get方法本身是可以访问generateClosure范围变量的,因此get执行的时候是有闭包的。
    但是上面的方法返回的是get返回的值,因为返回的不是函数,就没有闭包的概念了。
      

  5.   

    window外不是还有一层。
    all JavaScript functions are closures
      

  6.   

    这个问题的核心就是,如果外层函数不返回任何值,内层函数还算不算是闭包?答案貌似是算闭包。以下摘自Mozilla(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures)Consider the following:function init() {
        var name = "Mozilla"; // name is a local variable created by init
        function displayName() { // displayName() is the inner function, a closure
            alert (name); // displayName() uses variable declared in the parent function    
        }
        displayName();    
    }
    init();init() creates a local variable name and then a function called displayName(). displayName() is the inner function (a closure) — it is defined inside init(), and only available within the body of that function .
      

  7.   

    看不看的懂英文
    all JavaScript functions are closures;看不看的懂我的注释
    window外不是还有一层。看不看的懂我的回答
    你基本上对象都没选上。
      

  8.   


    看不看的懂英文
    all JavaScript functions are closures;
    --------------------------------------
    看不懂啊,all是啥意思?看不看的懂我的注释
    window外不是还有一层。
    --------------------------------------
    看不懂啊,原来这句是注释看不看的懂我的回答
    你基本上对象都没选上。
    --------------------------------------
    看不懂啊,从闭包能扯到对象太强了
      

  9.   

    not object
    is target
    should rely on contextual
      

  10.   


    What a F***ing hard discussion! But it seems you are the only one who gives the right answer at #2. Anyway, take the score and thank you.