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

Analysis of the occasions and advantages and disadvantages of js closure_javascript skills

WBOY
Release: 2016-05-16 15:53:39
Original
1361 people have browsed it

First the code:

//函数a
function a()
{
var i=0;
//函数b
function b()
{
alert(++i);
}
return b;
}
//函数c
var c = a();
c();

Copy after login

Code features:

1. Function b is nested inside function a;
2. Function a returns function b.
When the internal function b of function a in the code is referenced by a variable c outside function a, this is called creating a closure. Sometimes function b can also be returned by an anonymous function, that is, return function(){};

Advantages: 1. Protect the security of variables within the function and enhance encapsulation. 2. Maintain a variable in the memory (using too much will become a disadvantage and occupy memory)
The reason why closures occupy resources is that when function a ends, variable i will not be destroyed because function a ends, because the execution of b depends on the variables in a.
Not suitable for the scenario: the function that returns the closure is a very large function

The typical framework for closure should be jquery.

Closures are a major feature of the JavaScript language. The main uses of closures are to design private methods and variables.
This is more obvious when making a framework. Some methods and attributes are only used in the operation logic process. You don’t want to allow external modification of these attributes, so you can design a closure to only provide method access.

The disadvantage of closures is that they are resident in memory, which will increase memory usage. Improper use can easily cause memory leaks.

To summarize:

Advantages:

1. Logical continuity. When the closure is used as a parameter of another function call, it prevents you from breaking away from the current logic and writing additional logic separately.
2. Facilitate calling local variables of the context.
3. Strengthen encapsulation. An extension of point 2 can protect variables.

Disadvantages:

Closures have a very serious problem, which is the problem of memory waste. This memory waste is not only because it is resident in memory, but more importantly, improper use of closures will cause the generation of invalid memory. See below Example:

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]);

Copy after login

Through the above test, we will see that the addresses of the same logical closure generated by the execution of abc() ten thousand times are not the same, which means that it produces a bunch of identical Function objects, so there are The advantage is that you can use factory mode to generate functions for use. However, if you accidentally use closures as normal logic, memory garbage will inevitably be caused. It may be easier to understand if you change the syntax:

var foo = new Function();

Copy after login

So regarding closures, as far as my own habits are concerned, I don’t use them if I can. If they must be used, then find a way to keep the number of closure objects small or even unique.

The above is the entire content of this article, I hope you all like it.

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