This article mainly introduces the use of JS closures and advantages and disadvantages of the analysis, very detailed use, the need for small partners can refer to.
First previous code:
?
1 2 3 4 5 6 7 8 9 10 11 12 13-14 |
Functions a function A () {var i=0;//functions B Function B () {alert (++i);} return B;}//function c var c = a (); C (); |
Code Features:
1. The function b is nested inside function A;
2, function a returns function B.
The internal function B of function A in code is referenced by a variable C outside of function A, which is called creating a closure. Sometimes function B can also be returned with an anonymous function, that is, return function () {};
Advantages: 1. The security of the variable in the protection function strengthens the encapsulation 2. Maintain a variable in memory (with too much to become a disadvantage, accounting for memory)
The reason a closure takes up resources is that when function A is finished, the variable i is not destroyed because of the end of function A, because the execution of B depends on the variables in a.
Not suitable for scenario: a function that returns a closure is a very large function.
The typical framework for closures is jquery.
Closures are a major feature of JavaScript language, and the main applications of closures are designed to: design private methods and variables.
This is more obvious when doing the framework, some methods and properties are only used in the operation logic, do not want to let the external modification of these properties, so you can design a closure to provide only method access.
The drawback of closures is resident memory, which increases memory usage, and can easily lead to memory leaks due to improper use.
To sum up:
Advantages:
1. Logic continuity, when the closure as a parameter of another function call, avoid you from the current logic and write the additional logic alone.
2. Convenient to call the context of local variables.
3. Enhance the encapsulation, the 2nd extension, can achieve the protection of the variable.
Disadvantages:
The closure has a very serious problem, that is the memory waste problem, this memory waste not only because of its resident memory, more importantly, improper use of closures will cause invalid memory, look at the following example:
?
1 2 3 4 5 6 7 8 9 10 11 12-13 |
var array = []; Function abc () {var foo = function () {} array.push (foo); return foo;} for (var i = 0; I < 10000 i + +) {ABC ();} Alert (array[0] = = array[1]); |
Through the above test we will see that this 10,000 times ABC () execution produced by the same logical closure address is not the same, that is, it produced a bunch of identical function objects, so that the advantage is that the factory model to generate functions for use, However, if you accidentally use closures as normal logic, it will inevitably result in memory garbage. A new syntax may be better understood:
?
1 |
var foo = new Function (); |
So about closures, in my own habits, can not be used, if not, then find ways to keep the number of closures is very small or even unique.
The above mentioned is the entire content of this article, I hope you can enjoy.