Js closure usage and Advantages and Disadvantages analysis, js usage advantages and disadvantages

Source: Internet
Author: User

Js closure usage and Advantages and Disadvantages analysis, js usage advantages and disadvantages

Previous Code:

// Function afunction a () {var I = 0; // function bfunction B () {alert (++ I);} return B ;} // function cvar c = a (); c ();

Code features:

1. function B is nested in function;
2. function a Returns function B.
In the Code, when function a's internal function B is referenced by a variable c outside function a, it is called creating a closure. Sometimes function B can be returned with an anonymous function, that is, return function (){};

Advantages: 1. Protect the security of the variables in the function and enhance the encapsulation. 2. Maintain a variable in the memory (too many variables become a disadvantage, occupying the memory)
The reason why the closure occupies resources is that after function a finishes its execution, variable I will not be destroyed because function a ends, because the execution of function B depends on the variables in function.
Unsuitable scenario: the function that returns the closure is a very large function.

The typical closure framework should be jquery.

Closure is a major feature of the javascript language. The main application of closure is mainly to: Design private methods and variables.
This is more obvious in the framework. Some methods and attributes are only used in the logic process and do not want to be modified externally, therefore, you can design a closure to only provide method acquisition.

The disadvantage of the closure is the resident memory, which increases the memory usage. improper use may easily cause memory leakage.

Summary:

Advantages:

1. Logical continuity. When a closure is called as a parameter for another function, you do not need to write additional logic separately from the current logic.
2. It is convenient to call local variables of the context.
3. Enhanced encapsulation and 2nd point extension can protect variables.

Disadvantages:

There is a very serious problem with the closure, that is, memory waste. This memory waste is not only caused by its resident memory, but more importantly, improper use of the closure will lead to invalid memory, see the following 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]);

Through the test above, we can see that the closure addresses of the same logic produced by the 10 thousand abc () execution are different, that is, it produces a bunch of identical Function objects, the advantage is that the function can be generated in the factory mode for use. However, if you accidentally use the closure as the regular logic, it will inevitably cause memory garbage. It may be better understood to change the Syntax:

var foo = new Function();

So with regard to closures, as far as my habits are concerned, we don't need to use them if we don't need them. If we don't need them, we can find a way to keep the number of closure objects very small or even unique.

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

Related Article

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.