Advantages and disadvantages of using JavaScript to create a closure and difference analysis _ javascript skills

Source: Internet
Author: User
This article mainly introduces the advantages and disadvantages of the two methods for creating a closure in JavaScript and the related information for difference analysis. if you need it, you can refer to the two methods that are commonly used to create a closure in JavaScript.

Constructor method:

New function () {var variable ...}

Inline execution method:

(Function () {var variable ...})();

What are their differences in the JavaScript internal operating mechanism? Which method is better to create? What is its advantage over the closure created by other methods?

I understand it as follows:

Differences:

First: Sub-methods can share variables.
Second: internal sub-methods share variables

Comparison:

I think inline is better;

Advantages:

In general, inline creation requires memory on demand, because only the locally executed variables are in the memory, and relevant and dependent code can be organized, in order to minimize the risk of unexpected interactions, the execution variables of all the methods to be constructed must be saved in the memory, which will affect the performance of the web page. we recommend that you delete the variables before exiting.

Of course, this is just my personal understanding. In fact, it is generally necessary to use closures to generate memory leaks especially in IE kernel browsers. it is best to finish using the variables.

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

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.