The concept of closures

Learning Javascript Closure Closure
is a difficulty of the Javascript language, and it is also its feature. Many advanced applications rely on closures.

The following are my study notes, which should be useful for Javascript beginners.

The scope of variables

To understand closures, you must first understand JavaScript's special variable scope.

The scope of variables is nothing more than two: global variables and local variables.

The peculiarity of the Javascript language is that global variables can be directly read inside the function.

  var n=999;

  function f1(){
    alert(n);
  }

  f1 (); // 999

On the other hand, the local variables inside the function cannot be read naturally outside the function.

  function f1 () {
    var n = 999;
  }

  alert(n); // error

There is one thing to note here, when declaring variables inside a function, be sure to use the var command. If you don't use it, you are actually declaring a global variable!

  function f1(){
    n=999;
  }

  f1 ();

  alert(n); // 999

Second, how to read local variables from outside?

For various reasons, we sometimes need to get local variables inside a function. However, as has been said before, under normal circumstances, this is not possible and can only be achieved through workarounds.

That is, inside the function, define another function.

  function f1(){

    var n=999;

    function f2(){
      alert(n); // 999
    }

  }

In the above code, the function f2 is included inside the function f1, and all local variables inside f1 are visible to f2. But not vice versa, the local variables inside f2 are invisible to f1. This is the "chain scope" structure unique to the Javascript language, and the child object will look up all the variables of the parent object level by level. Therefore, all variables of the parent object are visible to the child object, but not vice versa.

Since f2 can read the local variables in f1, as long as f2 is used as the return value, can't we read its internal variables outside f1!

  function f1(){

    var n=999;

    function f2(){
      alert(n);
    }

    return f2;

  }

  var result = f1 ();

  result(); // 999

3. The concept of closure

The f2 function in the code in the previous section is the closure.

The definition of "closure" in various professional literature is very abstract and difficult to understand. My understanding is that a closure is a function that is able to read variables inside other functions.

Since in the Javascript language, only sub-functions inside functions can read local variables, so closures can be simply understood as "functions defined inside a function".

So, in essence, a closure is a bridge between the inside of the function and the outside of the function.

Fourth, the use of closures

Closures can be used in many places. It has two biggest uses, one is to read the variables inside the function mentioned above, and the other is to keep the values ​​of these variables in memory all the time.

How to understand this sentence? Please see the code below.

  function f1(){

    var n=999;

    nAdd=function(){n+=1}

    function f2(){
      alert(n);
    }

    return f2;

  }

  var result = f1 ();

  result(); // 999

  nAdd();

  result(); // 1000

In this code, result is actually the closure f2 function. It ran twice, the first time with a value of 999 and the second with a value of 1000. This proves that the local variable n in the function f1 has been kept in memory and is not automatically cleared after f1 is called.

Why is this so? The reason is that f1 is the parent function of f2, and f2 is assigned to a global variable, which causes f2 to always be in memory, and the existence of f2 depends on f1, so f1 is always in memory, not after the call ends. , collected by garbage collection mechanism (garbage collection).

Another notable place in this code is the line "nAdd=function(){n+=1}". First of all, the var keyword is not used before nAdd, so nAdd is a global variable, not a local variable. Secondly, the value of nAdd is an anonymous function, and the anonymous function itself is also a closure, so nAdd is equivalent to a setter, which can operate on local variables inside the function outside the function.

Five, the use of closure points

1) Because the closure will make the variables in the function are stored in the memory, the memory consumption is very large, so the closure cannot be abused, otherwise it will cause performance problems of the web page, which may lead to memory leaks in IE. The workaround is to delete all unused local variables before exiting the function.

2) The closure will be outside the parent function and change the value of the variable inside the parent function. So, if you use the parent function as an object, the closure as its public method, and the internal variable as its private value, be careful not to Feel free to change the value of the variable inside the parent function.

Six, thinking questions

If you can understand the running results of the following two pieces of code, you should understand the operation mechanism of closures.

Code snippet one.

  var name = “The Window”;

  var object = {
    name : “My Object”,

    getNameFunc : function(){
      return function(){
        return this.name;
      };

    }

  };

  alert(object.getNameFunc()());

Code snippet two.

  var name = “The Window”;

  var object = {
    name : “My Object”,

    getNameFunc : function(){
      var that = this;
      return function(){
        return that.name;
      };

    }

  };

  alert(object.getNameFunc()());

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324706380&siteId=291194637