_javascript techniques for closures in JavaScript

Source: Internet
Author: User

1, what is closure

Closures, the official explanation for closures, is that an expression (usually a function) that has many variables and an environment bound to them is also part of the expression.

Simply put, JavaScript allows the use of intrinsic functions---i.e. function definitions and function expressions in the function body of another function. Furthermore, these internal functions can access all the local variables, arguments, and other internal functions declared in the external function in which they are located. A closure is formed when one of these intrinsic functions is called outside the external function that contains them.

The characteristics of closure

1 function Nesting function

2 functions can refer to external parameters and variables inside

3 parameters and variables are not reclaimed by the garbage collection mechanism

After the normal function is finished, the local active object is destroyed and only the global scope is saved in memory. But the closures are different!

function fn () {
var a =;
function fn () {
//can access the A value
alert (a++) defined in FN;
}
fn ();
}
FN ();
fn ();// 
function fn () {
var a =;
function fn () {
//can access the A value
alert (a++) defined in FN;
}
Return fn;//
}
var f = fn ();
f (); After execution, a is still in memory
f ();//
F = null;//A is recycled

The above is a small series to introduce JavaScript in the closure of the package, I hope to help you!

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.