当前位置:网站首页>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
边栏推荐
- [Luke V0] verification environment 2 - Verification Environment components
- 653. Sum of two IV - input BST
- Number theory to find the sum of factors of a ^ B (A and B are 1e12 levels)
- Kettle experiment (III)
- Machine learning (VI) -- Bayesian classifier
- I don't understand time, timestamp and time zone. Look at this article
- [boutique] using dynamic agent to realize unified transaction management II
- Notes on xctf questions
- Group Backpack
- Little girl walking
猜你喜欢

To remember the composition ~ the pre order traversal of binary tree

Number of islands

Applet error: cannot read property'currenttarget'of undefined

#yyds干货盘点#ubuntu18.0.4安装mysql并解决ERROR 1698: Access denied for user ''root''@''localhost''

小程序报错:Cannot read property 'currentTarget' of undefined

Enter "net start MySQL" and "system error 5. Access denied" appears. Detailed explanation of the problem

Kettle实验 (三)

Leetcode-199 - right view of binary tree

Go language learning notes - slice, map | go language from scratch

Two methods of building Yum source warehouse locally
随机推荐
GoLand debug go use - white record
web页面如何渲染
Summary of wrong questions 1
小程序报错 :should have url attribute when using navigateTo, redirectTo or switchTab
Go language learning notes - exception handling | go language from scratch
[boutique] using dynamic agent to realize unified transaction management II
Your guide to lowering your cholesterol with TLC (continuously updated)
Get trustedinstaller permission
Base de la technologie électronique numérique 3.1 aperçu du circuit de porte, 3.2 circuit de porte à diode semi - conductrice
小女孩行走
Data visualization: use Excel to make radar chart
Applet error: cannot read property'currenttarget'of undefined
LeetCode396. Rotate array
Kettle experiment (III)
Strength comparison vulnerability of PHP based on hash algorithm
Go language learning notes - language interface | go language from scratch
I don't understand time, timestamp and time zone. Look at this article
A must see wechat applet development guide 1 - basic knowledge
kettle实验
基于ThinkPHP5版本TRC20-资金归集解决方案