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
Table of contents Install mockjs in your project ...
Table of contents Drop-down multiple-select box U...
Achieve results Code html <div class="css...
Preface I have an old laptop with Win7. In order ...
Table of contents 1. Timer monitoring 2. Event mo...
Preface The Boost library is a portable, source-c...
The following error occurred while installing the...
question: The following error occurred when insta...
Result: Implementation code: html <div class=&...
First, let's explain the network setting mode...
Recently, the Vue project needs to refresh the da...
Apache Tomcat is an open source software that imp...
Table of contents 1. What does shallow copy mean?...
This article example shares the specific code of ...
In the hive installation directory, enter the con...