当前位置:网站首页>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
边栏推荐
- 错题汇总1
- Yyds dry goods inventory ubuntu18 0.4 install MySQL and solve error 1698: access denied for user ''root' '@' 'localhost' '
- Mini - exercice MySQL (seulement pour les débutants, pas pour les non - débutants)
- ASUS laptop can't read USB and surf the Internet after reinstalling the system
- Is Zhongyan futures safe and reliable?
- GUI, CLI and UNIX Philosophy
- 【SQL server速成之路】数据库的视图和游标
- Production practice elk
- DVWA range practice
- AQS & reentrantlock implementation principle
猜你喜欢
A must see wechat applet development guide 1 - basic knowledge
Matlab draw five-star red flag
3、 6 [Verilog HDL] gate level modeling of basic knowledge
npm报错 :operation not permitted, mkdir ‘C: \Program Files \node js \node_ cache _ cacache’
Simple understanding of arguments in JS
Summary of wrong questions 1
Flink SQL realizes the integration of stream and batch
653. 两数之和 IV - 输入 BST
To remember the composition ~ the pre order traversal of binary tree
SAP 101K 411k inventory change
随机推荐
Resource packaging dependency tree
小程序报错 :should have url attribute when using navigateTo, redirectTo or switchTab
3、 6 [Verilog HDL] gate level modeling of basic knowledge
JS case to find the maximum value, reverse the array, bubble sort
MySQL - Chapter 1 (data types in MySQL)
MySQL of database -- Fundamentals
Trc20 fund collection solution based on thinkphp5 version
SAP 101K 411K 库存变化
1 + X cloud computing intermediate -- script construction, read-write separation
Secrets in buffctf file 1
Summary of common concepts and problems of linear algebra in postgraduate entrance examination
Distributed message oriented middleware framework selection - Digital Architecture Design (7)
小程序报错:Cannot read property 'currentTarget' of undefined
Number of islands
成功的DevOps Leader 应该清楚的3个挑战
MySQL of database -- basic common query commands
员工试用期转正申请书(泸州老窖)
基于ThinkPHP5版本TRC20-资金归集解决方案
Sql1 [geek challenge 2019]
Flink 流批一体在小米的实践