The situation of JS closure and the analysis of its advantages and disadvantages

Source: Internet
Author: User
Tags closure memory usage

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.