当前位置:网站首页>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
边栏推荐
- Applet error: should have URL attribute when using navigateto, redirectto or switchtab
- Vivo, hardware safe love and thunder
- DJ music management software pioneer DJ rekordbox
- Project upload part
- Kettle实验
- Failed to download esp32 program, prompting timeout
- Employee probation application (Luzhou Laojiao)
- LeetCode 1611. The minimum number of operations to make an integer 0
- Common errors of VMware building es8
- MySQL小練習(僅適合初學者,非初學者勿進)
猜你喜欢
Detailed explanation of delete, truncate and drop principles in MySQL database
Kettle experiment (III)
[geek challenge 2019] havefun1
MySQL小練習(僅適合初學者,非初學者勿進)
Buuctf [actf2020 freshman competition] include1
《信息系统项目管理师总结》第八章 项目干系人管理
Kettle实验
机器学习(六)——贝叶斯分类器
Yyds dry goods inventory ubuntu18 0.4 install MySQL and solve error 1698: access denied for user ''root' '@' 'localhost' '
What is augmented reality technology? Where can it be used?
随机推荐
kettle实验
[Luke V0] verification environment 2 - Verification Environment components
JS case to find the maximum value, reverse the array, bubble sort
Data visualization: use Excel to make radar chart
1 + X cloud computing intermediate -- script construction, read-write separation
DVWA range practice record
Number of islands
web页面如何渲染
Four pictures to understand some basic usage of Matplotlib
Go language learning notes - array | go language from scratch
MySQL small exercise (only suitable for beginners, non beginners are not allowed to enter)
MySQL of database -- overview and installation
【读书笔记】《Verilog数字系统设计教程》 第5章 条件语句、循环语句和块语句(附思考题答案)
搞不懂时间、时间戳、时区,快来看这篇
About CIN, scanf and getline, getchar, CIN Mixed use of getline
NPM reports an error: operation not allowed, MKDIR 'C: \ program files \ node JS \ node_ cache _ cacache’
Arbre de dépendance de l'emballage des ressources
Get trustedinstaller permission
NLLLoss+log_ SoftMax=CE_ Loss
MySQL - Chapter 1 (data types in MySQL)