JavaScript's closure JS variable scope

Source: Internet
Author: User
Tags closure variable scope

JS's closure

The variable scope of JS:

var // define a global variable    function Test () {            a//    }        Test ();    document.write ("a=" +a);
var // define a global variable    function Test () {            var// define a local variable     }        Test ();    document.write ("a=" +a);
// without Var, try to find (a of the parent function) and create a If you can't find it    . function Test () {        a// does not have Var, will try to find (parent function/outer layer of a), if not found, create a    }      Test ();    document.write ("a=" +a);

1. JS allows functions in the function

2. If you use a variable, if the variable has VAR, it means to create a completely new variable, if the variable is written directly on the page, then we think it is a global variable, otherwise is a local variable, if not with the Var keyword, then the JS engine will do this: first to the parent function to find out if there is a variable, If not, create it.

function F1 () {  //var n=999;//local variable        n=999;   global variable }       F1 ();   alert (n);

3. The above code shows that if in a function, the direct use of such as n=900; Equivalent to creating a global variable.

How to solve the local variables that read the internal functions from the outside in JS

function test1 () {      var n=90;   layout variable    // error

Workaround - Closed Package

 function   Test1 () { var  n=90;  // TEST1 function, you can access var n  
   
     Funciton test2 () {window.alert (n  ++
     //  return the inner function test2 to the external caller  return   test2; 

var res=test1 ();// called Test1, return test2 function At this Res is that test1 intrinsic Functions test2 aliases

Res ();

Closed Package : you can understand that . : Test2 () implementing Closures

u The main use of closures is

1. Keep local variables in memory, not garbage collection mechanism, recycle them .

2. allow external access to the layout variables of the inner function .

Sometimes you want N to accumulate, and you don't want the garbage collection mechanism to recycle it.

Closed Package

※ Note points for using closures

1) Because the closure allows variables in the function to be stored in memory, memory consumption is very large, so can not abuse closures, or cause Web page performance problems, in IE browser may cause memory leaks. Workaround, remove all unused local variables before exiting the function.

2) The closure will change the value inside the function outside the parent function. So, if you use the parent function as an object, take the closure as its common method, and take the internal variable as its private property (private value), be careful not to arbitrarily change the value of the inner variable of the parent function.

Function name cannot be duplicated in ※js

    1. In different files, you can have the same function name.
    2. In different parent functions (classes), even on the same page, you can have the same function, such as the following code abc.html

JavaScript's closure JS variable scope

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.