Scopes and closures in JavaScript _javascript tips

Source: Internet
Author: User
Tags closure garbage collection variable scope

One, JavaScript scopes

JavaScript variables actually have only two scopes, global variables, and internal variables for functions. A variable (var scope) defined anywhere within a function is scoped to the entire function body.
Global variable: Refers to the object properties under the Window object.
Scope Partitioning: Based on context, divided by function, not by block.
Emphasize two points:
1. In the same scope, JavaScript is a duplicate definition of the allowable variable, and the latter definition overrides the previous definition.
2. Variables defined within the function without the keyword VAR, default to global variables.

var scope= "global"; 
function T () { 
  console.log (scope);//"Global" 
  scope= "local" 
  console.log (scope);//' Local ' 
} 
t ( ); 
Console.log (scope); "Local" 




var scope= "global"; 
function T () { 
  console.log (scope);//"undefined" 
  var scope= "local" 
  console.log (scope); 
} 
T (); 
Console.log (scope); "Global" 

In the variable parsing process, first look for the local scope, and then find the upper scope. There is no scope defined in the function of the first code, and then the upper scope (global scope) is looked up to output its value. However, in the function of the second code defines the scope of the variable (whether after the console or before the definition of the variable, it is assumed that the scope has variable scope), and then no longer to the upper scope of the lookup, direct output scope. Unfortunately, the local variable i is not assigned a value at this time, so the output is undefined.

So according to the function scope meaning, the second paragraph of the above code can be rewritten as follows: 
var scope= "global"; 
function T () { 
  var scope; 
  Console.log (scope); 
  Scope= "Local" 
  console.log (scope); 
T (); 

Because of the characteristic of the function scope, the local variable is always defined by the whole function body, we can declare the variable "ahead" to the top of the function body.

var b; Step 1th 
Function Fun () {  
  b = ' change ';  
}  
alert (b);//output undefined, since step 1th defines only unassigned 
 
 
var B;//step 1th 
Function Fun () {  
  b = "Change";  
} 
Fun (); Call the above function 
alert (b);//output change 

When you declare a variable with VAR, the property that you create is not configurable, meaning it cannot be deleted by the delete operator.
Ii. Scope Examples

  
  
 

When the registration event is over, the value of I is 4, and when the button is clicked, the event function () {Alert ("button" +i);} This anonymous function does not have I, according to the scope chain, so go to the Buttoninit function, the value of I is 4, so Pop "Button4".
Three, JavaScript closures
in JS, closures mainly involve several other features of JS: Scope chain, garbage (memory) recycling mechanism, function nesting, and so on.
1. Scope chain: In short, the scope chain is what the function creates when it is defined, used to find an index of the value of a variable used, and his internal rule is to place the function's own local variable at the front, putting the variable in its parent function next, and putting the variable in the higher-level function behind , and so on until the global object. When the function needs to query the value of a variable, JS interpreter will go to the scope of the chain to find, from the first local variables to find, if not found the corresponding variable, then to the next level of the chain to find, once the variable has been found, then no longer continue. If the desired variable is not found, the interpreter returns undefined.
2. JavaScript garbage collection mechanism: in JavaScript, if an object is no longer referenced, the object is reclaimed by GC. If two objects are referenced to each other and are no longer referenced by the 3rd, then the two referenced objects are also reclaimed. Because function A is referenced by B, B is referenced by C outside of a, which is why function A is not recycled after it executes. Build a closure that will not be reclaimed by the memory collector, and will only be destroyed if the internal function is not invoked, and no variables referenced by the closure will be reclaimed by the next memory recycle startup.
3. With closures, nested function structures can operate
four, use JS closure to implement cyclic binding events

 

The above is the entire content of this article, I hope to learn JavaScript program to help you.

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.