当前位置:网站首页>JS scope, scope chain, global variables and local variables
JS scope, scope chain, global variables and local variables
2022-04-23 09:27:00 【Small white egg tart】
Catalog
3、 ... and 、 Depending on the scope , Variables can be divided into two types
One 、Javascript Scope
Is that the code name works and works in a certain range . In order to improve the reliability of the program, it is more important to reduce the naming conflict .
es6 Before : Global scope and local scope .
1、 Global scope
Whole script label , Or a separate js file .
2、 Local scope
Inside the function is the local scope , The name of this code only works inside the function .
Two 、 Scope chain
Just the code , There is at least one scope , The local scope written inside the function , If there are functions in the function , Then a scope can be created in this scope .
According to this mechanism that external function variables can be accessed in internal functions , Use chain search to determine which data can be accessed by internal functions , It's called scope . The principle is the principle of proximity .
The following code output 12 , Also note that functions are executed only when called . Only execute to statement f2() when , To execute functions f2() The statement in .
var num = 23;
function f1() {
var num = 12;
function f2() {
console.log(num);
}
f2();
}
f1();
3、 ... and 、 Depending on the scope , Variables can be divided into two types
1、 Global variables
Variables that are globally scoped , Variables defined outside functions , You can use... Anywhere in your code .num It's a global variable ,b It's also , Because if inside a function , Variables that do not declare direct assignment are also global variables .
<script>
var num = 20;
console.log(num);
function fn() {
var a = 20;
b = 30;
console.log(a);
}
</script>
2. local variable
Variables inside functions , Can only be used inside a function .a It's a local variable , Formal parameters are also local variables .
// Local variables are destroyed after the program is executed , Relatively economical
// Global variables are destroyed only when the browser is closed , More resources
版权声明
本文为[Small white egg tart]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230624114327.html
边栏推荐
- JSON input of Chapter 14 of kettle paoding jieniu
- Using JS to realize a thousandth bit
- Base de la technologie électronique numérique 3.1 aperçu du circuit de porte, 3.2 circuit de porte à diode semi - conductrice
- Installation of data cleaning ETL tool kettle
- Kettle实验 (三)
- Two ways for flutter providers to share data
- 成功的DevOps Leader 应该清楚的3个挑战
- To remember the composition ~ the pre order traversal of binary tree
- 基于ThinkPHP5版本TRC20-资金归集解决方案
- Program, process, thread; Memory structure diagram; Thread creation and startup; Common methods of thread
猜你喜欢

Pre parsing of JS

Emuelec compilation summary

Failed to download esp32 program, prompting timeout

Leetcode-199 - right view of binary tree

成功的DevOps Leader 应该清楚的3个挑战

Chapter VIII project stakeholder management of information system project manager summary

What is augmented reality technology? Where can it be used?

【SQL server速成之路】数据库的视图和游标

SAP 101K 411K 库存变化

MySQL of database -- overview and installation
随机推荐
Notes on xctf questions
Using sqlmap injection to obtain the account and password of the website administrator
Thread scheduling (priority)
【SQL server速成之路】数据库的视图和游标
RSA encryption and decryption signature verification
Four pictures to understand some basic usage of Matplotlib
Single sign on SSO
Kettle experiment (III)
Two declaration methods of functions of JS
LeetCode 1611. The minimum number of operations to make an integer 0
How to read excel table to database
Flink 流批一体在小米的实践
Go language learning notes - language interface | go language from scratch
Two ways for flutter providers to share data
112. 路径总和
Mini - exercice MySQL (seulement pour les débutants, pas pour les non - débutants)
Kettle experiment
1 + X cloud computing intermediate -- script construction, read-write separation
Give the method of instantiating the object to the new object
Kettle experiment