当前位置:网站首页>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
边栏推荐
- Four pictures to understand some basic usage of Matplotlib
- What is monitoring intelligent playback and how to use intelligent playback to query video recording
- 【SQL server速成之路】数据库的视图和游标
- 阿里云架构师解读四大主流游戏架构
- Installation of data cleaning ETL tool kettle
- 108. Convert an ordered array into a binary search tree
- tsdf +mvs
- RSA encryption and decryption signature verification
- LeetCode_ DFS_ Medium_ 1254. Count the number of closed islands
- Thread scheduling (priority)
猜你喜欢
Arbre de dépendance de l'emballage des ressources
Applet error: cannot read property'currenttarget'of undefined
[in-depth good article] detailed explanation of Flink SQL streaming batch integration technology (I)
[C language] document operation
Kettle experiment conversion case
Flink SQL realizes the integration of stream and batch
Go language learning notes - slice, map | go language from scratch
Sql1 [geek challenge 2019]
小程序报错 :should have url attribute when using navigateTo, redirectTo or switchTab
Kettle experiment (III)
随机推荐
亚马逊云科技入门资源中心,从0到1轻松上云
Get trustedinstaller permission
ALV树(LL LR RL RR)插入删除
【读书笔记】《Verilog数字系统设计教程》 第5章 条件语句、循环语句和块语句(附思考题答案)
Employee probation application (Luzhou Laojiao)
[Luke V0] verification environment 2 - Verification Environment components
小程序报错 :should have url attribute when using navigateTo, redirectTo or switchTab
Flink 流批一体在小米的实践
JSON input of Chapter 14 of kettle paoding jieniu
Program, process, thread; Memory structure diagram; Thread creation and startup; Common methods of thread
Principle of synchronized implementation
Is Zhongyan futures safe and reliable?
Simple understanding of arguments in JS
SAP 101K 411K 库存变化
What is monitoring intelligent playback and how to use intelligent playback to query video recording
LeetCode_ DFS_ Medium_ 1254. Count the number of closed islands
Kettle experiment
How to render web pages
Kettle experiment (III)
Single sign on SSO