Javascript closure and scope, javascript Scope

Source: Internet
Author: User

Javascript closure and scope, javascript Scope

Today we will talk about the use of javascript scopes and closures. Let's first talk about the concept of scope. Various Development Speech will encounter the life cycle of variables. The scope of use is actually the scope of variables. Literally, this variable is valid within that code range.

1. The Javascript scope is the function scope rather than the block-level scope. Check the Code:

(Function () {var I = 1; if (I = 1) {var j = 3;} console. log (j); // can be accessed here })()

Here, j is accessible, that is, variables defined at any position in a function are visible anywhere in the function. Let's look at another situation:

function a(){    function b(){        //code    }}
The variables declared in method a can also be used in method B. The principle is the same as above.

2. The scope of variables in Javascript is divided into global variables and local variables. See the Code:

var p=11;function f1(){   console.log(p);}f1();   //11
The variable declared outside the method body is a global variable, which can be accessed in any method. Let's look at another global variable declaration method:

function f1(){    p=11;}f1();console.log(p);  
Since p = 11 is declared in the method body, it is equivalent to having the global variable p, so this declaration method is also acceptable, but it is not recommended.

3. How can I access and operate the variables in the function? Here we need to use closures.

Let's take a look at the official explanation of the closure:A closure is an expression (usually a function) that has many variables and is bound to these variables. Therefore, these variables are part of the expression.

I can't help but ask, what is this?

Later, I started to understand it by referring to some blogs and "Javascript Secret Garden". The closure is probably because a function inside the function is called externally, so that internal variables can be called. For example, the following section

function f1(){    var p=11;        return {            increment: function() {                p++;            },                 show: function() {                alert(p)            }        }}var f=f1();f.show();      //11f.increment();f.show();      //12
This method is similar to the get set Method for private variables in java bean.


Closure and scope issues in javascript

Vartimeout = setInterval (function (){

$ ('Button # mybutton '). click (function (){
ClickCount ++;
});
Time --; // timing
}, OneSecond );

In this section, multiple $ ('button # mybutton '). click are added, and the unbind event is not displayed, so the problem will certainly occur.
A little messy

Javascript Closure

The js closure problem is actually the scope of js variables. js has two scopes: global scope and function scope, so the global scope will not be mentioned, closures are actually a feature derived from function scopes. because each function has a scope, a scope chain exists. For example:
Var a = "";
Function test0 (){
Var B = "B ";
Function test1 (){
Var c = "c ";
Return a + B + c;
}
}
In this way, a scope chain has a global scope ==> test0 function scope ==> test1 function scope, so that the test1 function can obtain the values of test0 and global variables, that is, the values of a and B can be obtained. The test0 function can obtain the global variable value, that is, the value following the scope chain can be obtained. in fact, the closure refers to this feature of the scope chain.
However, closures are generally used on anonymous functions. The above functions can be changed to the following
Var a = "";
Function test0 (){
Var B = "B ";
Return function (){
Var c = "c ";
Return a + B + c;
}
}
Test0 (); // => "abc"
I hope it will 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.