1. ScopeGenerally speaking, the names used in a program code are not always valid and available, and the scope of code that limits the availability of the name is the scope of the name. The use of scope improves the locality of program logic, enhances program reliability, and reduces name conflicts. There are two types of scopes in JavaScript (before es6):
1. Global scope Applies to the environment where all code is executed (the entire 2. Local scopeThe code environment that acts on a function is the local scope. Because it is related to functions, it is also called function scope. For example: for(let i=0;i<100;i++){ sum += i; } 2. Scope of variablesIn JavaScript, variables can be divided into two types according to their scope:
1. Global variables Variables declared in the global scope are called global variables (variables defined outside a function). 2. Local variables Variables declared in a local scope are called local variables (variables defined inside a function) 3. The difference between global variables and local variables
3. Scope ChainAccording to the mechanism that inner functions can access outer function variables, chain search is used to determine which data can be accessed by inner functions, which is called scope chain.
function f1() { var num = 123; function f2() { console.log( num ); } f2(); } var num = 456; f1(); The analysis is shown in the figure below: It can be seen that the final result is: 123 Similarly, the final value of the variable can also be found by adopting the proximity principle. This is the end of this article about the details of You may also be interested in:
|
<<: HTML end tag issue and w3c standard
>>: Solution to abnormal connection table caused by inconsistent MySQL character sets
I just learned some html yesterday, and I couldn...
Table of contents 1. Node builds HTTP server 2. H...
Recently, our small team needs to share a shared ...
This article example shares the specific code of ...
Preface Recently I encountered a deadlock problem...
This article shares the specific code of JavaScri...
Preface Recently, I was working on a report funct...
Share a beautiful input box animation style libra...
Table of contents Preface Background Implementati...
Table of contents 1. What is lazy loading of rout...
The author recently encountered a performance bot...
Table of contents Storage Engine Storage engines ...
Table of contents Previous words Usage scenarios ...
The main symptom of the conflict is that the FLASH...
Introduction Memcached is a distributed caching s...