Precise about the closure and memory leaks memory leaks caused by the closure of how to solve?

Closures (closure) is a major difficulty javascript, but also its characteristics. Many advanced applications must rely on to achieve closure. We divided the following points of view.

1, variable scope

To understand the closures, we must first understand the special variable scope of javascript.

Scoped variables nothing more than two types: global and local variables.

Special javascript language is that: internal function Global variables can be read directly, but can not read the local variables inside a function outside the function.

Precautions: When you declare a variable inside a function, be sure to use the var command. If you do not, you are actually declare a global variable!

2, how to read a local variable inside a function from the outside?

For various reasons, we sometimes need to get to a local variable inside the function. But already it said, under normal circumstances, it is impossible! It can only be achieved through alternative methods.

That is, inside the function, and then define a function.

 

function f1 () { 

    were n = 999; 

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

  }

 

In the above code, the function f2 are included in the internal functions f1, f1 time all local variables inside of f2 is visible. But the reverse is not, local variables inside of f2, f1 is to be invisible.

This is the Javascript language-specific "scope chain" structure (chain scope),

Child objects will look for an upward all the variables of a parent object to. So, all variables parent object, child objects are visible, not vice versa.

Since f1 f2 can be read in a local variable, so long as f2 as a return value, we can not read its internal variables outside f1 yet!

3, the concept of closures

Code above function f2 is the closure.

A variety of professional literature closure definitions are very abstract, I understand that: the closure is able to read the other functions of the internal variable function.

Since in javascript, only the internal function subroutine to read the local variables, so that the closure can be simply understood as a "function defined inside a function."

Therefore, in essence, the closure internal bridge function and connecting external functions.

4, the use of closures

Closures can be used in many places. It's most useful for two, one is the aforementioned variables can be read inside the function, and the other is to make the values ​​of these variables remain in memory and are not automatically cleared after f1 call.

Why is this so? The reason is that the parent function f1 is f2, and f2 is assigned to a global variable, which leads to f2 is always in memory, and relies on the presence of f1 and f2, f1 therefore always in memory, and not after the end of the call , the garbage collection (garbage collection) recovered.

5, the use of closures Precautions

(1) Due to the closure will make the function of the variables are stored in memory, memory consumption is large, it can not be abused closure, otherwise it will cause performance problems webpage in IE may lead to memory leaks. The solution is, before exiting the function will delete all local variables are not used.

(2) closure will function outside the parent, the parent function changes the value of the internal variable. So, if you take the parent function as an object (object) to use, the closure of its public methods (Public Method) as, the internal variable as its private property (private value), then you must be careful not to just change the value of the parent function of internal variables.

Closures cause memory leaks how to solve?

1 What is a memory leak?

1. Definitions: a memory allocated neither used nor recovered. Which could affect performance, or even cause the program to crash.

 2. Causes: JavaScript garbage collection mechanism to automatically identify those variables will no longer continue to use certain strategies to release its occupied memory. However, due to some reasons such a mechanism in the memory manager can not correctly interpret the life cycle of JavaScript variables, and thus did not release its memory, but no longer be used.

 Circular references is one of the main cause of the above.

2 solution

Common solution is finished at the time of running JavaScript code segments forming a circular reference JavaScript object manually set is empty cut references.

 

About closures and memory leaks is that these, I hope to give you help, please correct me to discuss

Inspired by the https://www.cnblogs.com/peko/p/7487047.html and https://www.cnblogs.com/cxying93/p/6103375.html . Thanks to two

Guess you like

Origin www.cnblogs.com/wzfwaf/p/10738771.html