The front end of knowledge: JavaScript Basics - scopes and closures - the principle and the role of closures and stack overflows and memory leaks principles and corresponding solutions

The principle and the role of closures

Closure:

The right to access another function of the variable function scope.

Creating closures common way is to create a function in another function.

Packages are closed:

Access internal variable function, the function has been kept in the environment, will not be processed garbage collection

Because the variable declared inside a function are local, they can only be accessed inside a function, but a function of external variables is visible inside the function, which is characteristic of the scope chain.

Children can look up variables to the parent, step by step look, find the date

Therefore, we can then create a function inside a function, so that the internal function, the variables of the outer function is visible, then we can have access to his variables.

<Script>
     function   bar () {
         // outer function declaration variables 
        var value =. 1 ;
         function foo () {
            console.log(value);
        }
        return foo();
    };
    var bar2 = bar;
     // actually bar () function does not complete because the execution was processed out of the garbage collection mechanism 
    // role which is closure, calling bar () function, which will execute the function foo, foo which when the outer layer will have access to the variable 
    bar2, ();
 </ Script>

foo () containing bar () inside the scope of the closure, so that the scope has been able to survive, not to dispose of garbage collection, this is the role of closures, for foo () referenced at any time.

Closure package advantages:

  • Easy call local variables declared in the context of
  • Logic closely, you can then create a function within a function, to avoid the problem of parameter passing

Closure package disadvantages:

Because the use of closures, the function can not be destroyed after the execution, it remains in memory, if a large number of closures will result in the use of memory leaks, memory consumption great

to sum up:

Closures can access external variable function, even if the variables have left the environment in which it was created, because the variables are outside the scope of the object closure held. This feature implements closure implicit data transfer between the nested function.

Closure Application:

function addFn(a,b){
    return(function(){
        console.log(a+"+"+b);
    })
}
var test =addFn(a,b);
setTimeout(test,3000);

The first parameter is a function of setTimeout generally, but not traditional values. If you want to pass a value into it, you can call the function to return a call to an internal function, will call the internal function passed to setTimeout. Perform the required function parameters internal, external functions to him, in setTimeout function can also access to external functions.

Stack overflow and memory leaks principle:

1, memory leaks: refers to the memory after the execution of the application is not timely clean-up or destruction of, occupy free memory, memory leaks too much, it would lead to the back of the program are not eligible for memory. Therefore, memory leaks may result in internal memory overflow

2, stack overflow: refers to the application memory space has been completed, there is not enough memory provides

3, in some programming software, such as c language, we need to use malloc to allocate memory space, then use the free release off manually cleared. The js is its own garbage collection, garbage collection method commonly used marker is cleared.

Clear labeling method: Add to it a mark after a variable into the execution environment: variable into the environment, will not be released into the environment, as long as the "execution flow" into the response of the environment, it is possible to use them. After leaving the environment variable, it is marked as "leave the environment."

4, the common cause of memory leaks:

  • Memory leaks caused by global variables
  • Closure
  • Not cleared timer

5. Solution:

  • Reduce unnecessary global variables
  • Reduce the use of closures (due to closures would lead to a memory leak)
  • To avoid the occurrence of an infinite loop

Reference article:

https://blog.csdn.net/alegria_x/article/details/99539024

https://www.cnblogs.com/shiyou00/p/10598010.html

https://www.jianshu.com/p/9fc2e3ee4efe

Guess you like

Origin www.cnblogs.com/memphis-f/p/12061036.html