zoukankan      html  css  js  c++  java
  • 我也谈javascript闭包

    1、什么是闭包呢?
    Whenever you see the function keyword within another function, the inner function has access to variables in the outer function

    function foo(x) { var tmp = 3; return function (y) { alert(x + y + (++tmp)); // will also alert 16 } } var bar = foo(2); // bar is now a closure. bar(10);

    /*
    *    When a function is defined in another function and it
    *    has access to the outer function's context even after
    *    the outer function returns.
    *
    * An important concept to learn in JavaScript.
    */
    
    function outerFunction(someNum) {
        var someString = 'Hey!';
        var content = document.getElementById('content');
        function innerFunction() {
            content.innerHTML = someNum + ': ' + someString;
            content = null; // Internet Explorer memory leak for DOM reference
        }
        innerFunction();
    }
    
    outerFunction(1);​


    Two one sentence summaries:

    • a closure is the local variables for a function — kept alive after the function has returned, or
    • a closure is a stack-frame which is not deallocated when the function returns (as if a 'stack-frame' were malloc'ed instead of being on the stack!).

    The following code returns a reference to a function:

    function sayHello2(name) {
        var text = 'Hello ' + name; // Local variable
        var sayAlert = function() { alert(text); }
        return sayAlert;
    }
    say2 = sayHello2('Bob');
    say2();

    Most JavaScript programmers will understand how a reference to a function is returned to a variable in the above code. If you don't, then you need to before you can learn closures. A C programmer would think of the function as returning a pointer to a function, and that the variables sayAlert and say2were each a pointer to a function.

    There is a critical difference between a C pointer to a function and a JavaScript reference to a function. In JavaScript, you can think of a function reference variable as having both a pointer to a function as well as a hidden pointer to a closure.

    The above code has a closure because the anonymous function function() { alert(text); } is declared inside another function, sayHello2() in this example. In JavaScript, if you use the functionkeyword inside another function, you are creating a closure.

    In C, and most other common languages after a function returns, all the local variables are no longer accessible because the stack-frame is destroyed.

    In JavaScript, if you declare a function within another function, then the local variables can remain accessible after returning from the function you called. This is demonstrated above, because we call the function say2() after we have returned from sayHello2(). Notice that the code that we call references the variable text, which was a local variable of the function sayHello2().

    function() { alert(text); } // Output of say2.toString();

    Click the button above to get JavaScript to print out the code for the anonymous function. You can see that the code refers to the variable text. The anonymous function can reference text which holds the value 'Bob' because the local variables of sayHello2() are kept in a closure.

    The magic is that in JavaScript a function reference also has a secret reference to the closure it was created in — similar to how delegates are a method pointer plus a secret reference to an object.

     
     
  • 相关阅读:
    30流的使用和分类
    使用EF Model First创建edmx模型,数据库有数据的情况下,如何同时更新模型和数据库
    29防止程序集被篡改仿冒,全局程序集缓存GAC
    报错:不允许保存更改。您所做的更改要求删除并重新创建以下表……
    28先判断是否存在,再创建文件夹或文件,递归计算文件夹大小
    27程序集资源
    MVC缓存02,使用数据层缓存,添加或修改时让缓存失效
    26复杂类型比较,使用Compare .NET objects组件
    25LINQ拾遗及实例
    MVC缓存01,使用控制器缓存或数据层缓存
  • 原文地址:https://www.cnblogs.com/oxspirt/p/4435006.html
Copyright © 2011-2022 走看看