Details about the scopes and closures in javascript and about javascript
I. JavaScript Scope
JavaScript variables have only two scopes: global variables and internal variables of functions. The scope of a variable (var scope) defined in any part of a function is the entire function body.
Global variable: refers to the object attributes under the window object.
Scope Division: it is based on context and divided by functions, rather than blocks.
Emphasize two points:
1. In the same scope, JavaScript allows repeated definitions of variables, and the latter will overwrite the previous definition.
2. If the variable defined by the keyword var is not added to the function, the global variable is used by default.
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); //"local" } t(); console.log(scope); //"global"
In the variable parsing process, first find the local scope, and then find the upper scope. The scope variable is not defined in the function of the first code, so the upper-level scope (global scope) is searched and its value is output. However, the variable scope is defined in the function of the second code (whether after the console or before the variable is defined, it is considered that the scope has the variable scope ), therefore, the scope of the upper layer is no longer searched, and the scope is directly output. Unfortunately, at this time, the local variable I is not assigned a value, so the output is undefined.
// According to the function scope, you can rewrite the code in the second paragraph as follows: var scope = "global"; function t () {var scope; console. log (scope); scope = "local" console. log (scope) ;}t ();
Due to the function scope feature, local variables are always defined throughout the function body. We can declare variables "in advance" to the top of the function body.
Var B; // Step 2 function fun () {B = "change";} alert (B); // output undefined. Because step 2 defines only var B with no value assigned; // Step 2 function fun () {B = "change";} fun (); // call the above function alert (B); // output change
When a variable is declared using var, the property created cannot be configured, that is, it cannot be deleted using the delete operator.
II. Scope instances
After an event is registered, the value of I is 4. When you click the Button, the event function is called function () {alert ("Button" + I );} this anonymous function does not contain I. According to the scope chain, it is found in the buttonInit function. At this time, the value of I is 4, so "button4" is displayed.
Iii. javaScript Closure
In js, closures mainly involve several other features of js: Scope chain, garbage (memory) collection mechanism, function nesting, and so on.
1. scope chain: in short, the scope chain is an index created by a function during definition and used to find the value of the used variable. Its internal rule is, put the local variables of the function itself at the beginning, put the variables in its parent-level functions at the second place, put the variables in the higher-level functions at the back, and so on until the global object ends. When a function needs to query the value of a variable, the js interpreter searches for the value in the scope chain first from the local variable at the beginning. If the corresponding variable is not found, then go to the chain at the next level. Once the variable is found, it will not continue. If no variable is found, the interpreter returns undefined.
2. Garbage collection mechanism of Javascript: In Javascript, if an object is no longer referenced, it will be recycled by GC. If two objects are referenced by each other and no longer referenced by 3rd, the two objects referenced by each other will be recycled. Because function a is referenced by function B and function B is referenced by Function c outside of function a, this is why function a is not recycled after execution. Build a closure. These variables will not be recycled by the memory recycler. The closure will be destroyed only when internal functions are not called, variables referenced by no closure will be recycled at the next memory recycle startup.
3. With closures, nested function structures can work.
4. Implement cyclic binding events using js closures
<Html>
The above is all the content of this article, and I hope it will help you learn javascript programming.
Articles you may be interested in:
- JavaScript variable scope and Closure
- Analysis of javascript lexical scopes and closures
- Understand javascript_15_scope allocation and variable access rules, and then send a closure
- JavaScript. The. Good. Parts Reading Notes (2) Scope & closure & mitigation of global space pollution
- Scope chains and closures in JavaScript
- In-depth usage of Javascript Functions, recursion, and closures (execution environment, variable object, and scope chain)
- Javascript scopes and closures
- Deep understanding of javascript scopes and closures
- Five small examples that help you understand JavaScript core closures and scopes