Home > Web Front-end > JS Tutorial > body text

Understanding the closure principle of js

黄舟
Release: 2017-02-27 14:41:37
Original
1311 people have browsed it

question? What is the closure principle of js (javaScript) and what is its function?

1. Definition

Official explanation: A closure is an expression that has many variables and an environment that binds these variables (usually is a function), so these variables are also part of the expression.


Obviously, ya. . . . . What! Rural people can’t understand the door-smashing! ! !

So the editor’s understanding is this: **** is a function defined in a function and can be accessed externally. (Under normal circumstances we cannot access local functions)This is a bit like taking off your pants and farting. It is unnecessary, but it is not unnecessary. Closures definitely have their uses.


Function: 1. It can reduce the objects of global variables and prevent global variables from being too large in the past and making it difficult to maintain.

2. Prevent modifiable variables, because internal variables are inaccessible from the outside and cannot be modified. Safety

3. Read the variables inside the function. The other is to keep the values ​​of these variables in the memory.

2. Example: (js code)

1. The special thing about Javascript language is that the global function can be read directly inside the function variable.

var n=999;
  function f1(){
    alert(n);
  }
  f1(); // 999
Copy after login

2. On the other hand, local variables within the function cannot be read outside the function.


 function f1(){
    var n=999;
  }
  alert(n); // error
Copy after login

There is one thing to note here. When declaring variables inside a function, you must 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
Copy after login



##**** *How to read local variables from outside?


#We sometimes need to get local variables within a function. However, as mentioned before, this is not possible under normal circumstances and can only be achieved through workarounds.


##

function f1(){
    n=999;
    function f2(){
      alert(n); // 999
    }
  }
Copy after login

3. Use closure Notes on packages

1) Since closures will cause the variables in the function to be stored in memory, which consumes a lot of memory, closures cannot be abused, otherwise it will cause performance problems on the web page. In IE May cause memory leak. The solution is to delete all unused local variables before exiting the function.



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

change the value of the variable inside the parent function.




The above is the understanding of the closure principle of js. For more related content, please pay attention to the PHP Chinese website (www.php.cn )!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!