Illustrates a closure

Speaking of  closure  , which is a characteristic js must mention, many traditional languages do not have such characteristics, such as JAVA C and so on.

Before reading, always good to understand what is closure! Here's a hand-painted by the schematic to understand this:

  Firstly substantially all programming languages ​​have similar characteristics, the method can access local external attributes of the parent class method, i.e., subclass or sub-method can access a resource of the parent class.

<!-- 在正常的脚本中,某个方法可以获取到外部的变量,或者全局变量 -->
        var num = 11;
        function func1(){
            console.log(num);
        }
        func1();

  Therefore, the above code, we can get the value of num.

 

  The parent can get into the interior of the value of the sub-way to do that?

function func2(){
            var num1 = 22;
            num2 = 33;
        }
        func2();
        <!--console.log(num1);  会报错!-->
        console.log(num2); <!--可以获取到num2的值,因为不使用var定义变量时,默认是全局变量 -->

  Of course not, because the scope of the variable sub-method is only a method of sub-range, external can not be acquired.

  

  So how can you get outside to a local variable sub-method it!

  If java, a class of private property, can be obtained by the public get methods, such as:

class Person{
  private String name;
  public String getName(){
    return name;    
}    
}

  It can be obtained by the above manner to the inside of a kind of private property, the same, in the js local variables may be acquired by a method of this method, and to read the value of the variable desired by the method in this method.

function func3(){
            var num3 = 44;
            function func4(){
                return num3;
            }
            return func4;
        }
        var func = func3();
        console.log(func());

  Reference to the following illustration:

  Unable to get outside to the inside of the func3 local variables, but func3 local approach func4 inside but can get to, and therefore  returns a reference to a func4 of  , so you can get to the outside of the internal variables func3 through this func4.

  Although around a circle, but obtained by means of such a value to the interior of the external methods.

  The method func4 locally in this method is called the closure, many of the concepts according to the book, this method is built outside the internal method and bridge method, such that the interior of the external resource may be acquired any method.

  But the closure will result in lasting occupation variables in memory, so there will be some performance issues, it is best not to use easily, even with the right but also the actual release.

 

  Examples of Source:

<!doctype html>
<html>
    <head>
    </head>
    <body>
        <script type="text/javascript">
        <!-- 在正常的脚本中,某个方法可以获取到外部的变量,或者全局变量 -->
        var num = 11;
        function func1(){
            console.log(num);
        }
        func1();

        <!-- 但是在外部是无法获取方法内部的局部变量的 -->
        function func2(){
            var num1 = 22;
            num2 = 33;
        }
        func2();
        <!--console.log(num1);  会报错!-->
        console.log(num2); <!--可以获取到num2的值,因为不适用var定义变量时,默认是全局变量 -->

        <!-- 那么如何在外部获取到内部的变量呢!javascript可以办到 -->
        function func3(){
            var num3 = 44;
            function func4(){
                return num3;
            }
            return func4;
        }
        var func = func3();
        console.log(func());

        </script>
    </body>
</html>

  operation result:

Reproduced in: https: //my.oschina.net/u/204616/blog/545166

Guess you like

Origin blog.csdn.net/weixin_34221775/article/details/91989446