JS seal in-depth understanding

Source: Internet
Author: User
Tags javascript closure example

1. JavaScript language understanding Closures

The range of JS variables is divided into two: global variables, local variables. Variables are declared outside of a function of global variables, and internal functions can call global variables directly. Declaring a variable inside a function must use the var command, otherwise the function inside it declares a global variable.

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 able to enable the upper function to read the local variables of other functions.

  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 to be a function defined inside a function. Essentially, a closure is a bridge that connects the inside of the function with the outside of the function.

Use of closures: 1. Ability to read variables inside a 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 can not abuse the closure, otherwise the page performance will be reduced, in IE may also be memory leaks, so before exiting the function, will no longer use the local variable all delete.

JavaScript Closure 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 such as the following, that is, 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 in which it is defined, the inner function's closure is created, assuming that the intrinsic function refers to a variable that is located outside the function, and when the external function call is complete, the variables are not freed in memory because they are required by the closure.

Take another look at the example

JS Code

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

The result is 0,4 . Because Varkeyword is used inside the function, the scope of maintenance A is inside 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 describing a path that can be used along the path to determine the value of a variable. When running a=0, the assignment operation is linked to Var a=4 along the scope, because Varkeyword is not being employed; and change its value.

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


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

JS seal in-depth understanding

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.