JavaScript Basics-Closures

Source: Internet
Author: User

Principle

Functions contain functions, i.e. closures.

The result of the include function is that the child function will hijack the parent function's active object. When a child function accesses a variable, it first reads its own active object, whether it contains this variable, does not find it from the parent function, does not, go to the ancestral function, and layers back to the window, not returning to undefined.

Advantages

object, the basis of modularity.

Disadvantages

Additional memory overhead: the child function will hijack the parent function's active object before it is destroyed.

ie8-(IE8 and IE8) are prone to memory leaks: Ie8-'s DOM is written by COM, and standard browser (chrome,firefox,safari,opera) memory reclamation is reference cleanup, and ie8-is a token count. The difference between the two:

Tag count, once a circular reference (especially access to the DOM) is made, a memory leak is caused.

Closure optimization

When you are finished, assign null/undefined to the child function. Free up memory in a timely manner.

Example

jquery monolithic architecture with closures

(function(window, undefined) {      ...      attr:function() {      }      ...}) (window)

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.