Recursive and closures

Closure

1. Definitions: internal functions are functions (nested functions), the external local variables inside a function operation function, and the outer function is assigned to a reference variable, function by accessing the anonymous inner function variables.

2. Role: 1 can read a variable inside a function, so that 2 values ​​for these variables remain in memory and are not automatically cleared after f1 call.

3. Scenario: mainly used for packaging recycling bind events jQuery plugin, developed with less.

4. advantages: avoiding contamination variables.

5. Disadvantages: closures 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.

Recursion:

Definition: recursive function calls itself is a function (function from call) in the name of a function by function. 

Scenario: maintain a list of table set up, made a header filtering function. Where you need to get screened entry, when there are multiple filter arrays need to get items from the background, we need to loop through the array to send ajax request, but because ajax is asynchronous. Multiple requests can not process returns to normal. 
So using a recursive, he recalled in an ajax callback:

advantage:

Simplicity, traversal algorithm to achieve recursive loop is much simpler than the obvious

Disadvantages:

1. The present time and space consuming,

2. The call stack may overflow. Affect performance.

Difference recursive closure with: 
similarities: 1) is a function of 
    two) internal function will call the function

Different points: 1) the closure of the internal function function call external functions need to refer to the variable parameter; recursive function calls itself is not necessary reference variables, parameters 
     2) once the closure end call function call, recursive recursive function satisfying the conditions when several calls 
     3) closure function accounted for more memory than other functions

Guess you like

Origin www.cnblogs.com/mlw1814011067/p/11648290.html