In-depth understanding of JS closures

Source: Internet
Author: User

1. JavaScript-language closure comprehension

JS variables are scoped into two types: global variables, local variables. A variable declared outside a function is a global variable, and a global variable can be called directly inside a function. Declaring a variable inside a function must use the var command, otherwise it declares a global variable inside the function.

The closure is generated to read the local variables of the function from the outside, that is, in the function to define a function F2, the F2 as the return value, in the upper function of the return can be the upper function to read the other functions of the local variables.

  Function F1 () {

n=999;

function F2 () {
alert (n);
}

return F2;

}

var result=f1 ();

Result (); //999

Closures (closure) are functions that can read other functions ' internal variables. It can also be said that a function is defined inside a function, in essence, a closure is a bridge that connects the inside of the function with the outside of the function.

Use of closures: 1. Can read variables inside the function

2. Keep the values of these variables in memory, because F2 relies on the existence of F1.

Disadvantage: The variables in the function are stored in memory, so that the memory consumption is very large, so the closure can not be abused, otherwise the Web page performance will be reduced, in IE may also be memory leaks, so before exiting the function, the unused local variables are all deleted.

JavaScript Closures Example

function Outerfun ()
{
var a=0;
function Innerfun ()
{
a++;
alert (a);
}
}
Innerfun ()

The above code is wrong . The scope of Innerfun () is inside Outerfun () and it is wrong to call it outside the Outerfun ().

Change to the following, i.e. closures:

JS Code

function Outerfun ()
{
 var a=0;
 function innerfun ()
 {
  a++;
  alert (a);
 }
 return innerfun;  //note here
}
Var obj=outerfun ();
obj ();   //result is 1
obj ();   //result is 2
var obj2=outerfun ();
Obj2 ();   //result 1
obj2 ( );   //result 2

What is a closure package:

When an intrinsic function is referenced outside the scope that defines it, the inner function's closure is created, and if the intrinsic function references a variable that is located outside the function, when the external function is called, the variables are not freed in memory because the closures require them.

Let's look at one more example

JS Code

function Outerfun ()
{
var a = 0;
alert (a);
}
var a=4;
Outerfun ();
alert (a);

The result is 0,4 . Because the VAR keyword is used inside a function to maintain the scope of a within outfun ().

Then look at the following code:

JS Code

function Outerfun ()
{
//No var
A = 0;
alert (a);
}
var a=4;
Outerfun ();
alert (a);
The result is 0,0 really strange, why?

A scope chain is a term that describes a path that can be used to determine the value of a variable. When a=0 is executed, the assignment is linked to Var a=4 along the scope, because the VAR keyword is not employed; and change its value.

Http://www.jb51.net/article/24101.html


In-depth understanding of JS closures

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.