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.

     
     
  • 相关阅读:
    ASP.NET Core依赖注入(DI)
    SQLSERVER 创建索引视图注意事项
    Git的基本使用方法(0基础小白也能看懂)
    并发系列64章(异步编程二)第三章
    并发系列64章(异步编程)第二章
    并发系列64章(并发概要)第一章
    SQL Server配置邮件服务器
    SQL Server常用函数及命令
    SQL Server将一段字符串根据特定分隔符转换成一个表变量
    SQL Server双机热备之发布、订阅实现实时同步
  • 原文地址:https://www.cnblogs.com/oxspirt/p/4435006.html
Copyright © 2011-2022 走看看