当前位置:网站首页>JS scope
JS scope
2022-07-03 05:04:00 【The wind blows and birds fly in the crotch】
1.1 Scope Overview
Generally speaking , The names used in a piece of program code are not always valid and available , The scope of the code that defines the usability of the name is the scope of the name . The use of scope improves the locality of program logic , Enhance the reliability of the program , Reduced name conflicts . JavaScript(es6 front ) There are two kinds of scopes in :
Global scope
Local scope ( Function scope )
1.2 Global scope
The environment that acts on all code execution ( Whole script The label inside ) Or an independent js file .
1.3 Local scope
Act on the code environment within the function , It's a local scope . Because it has something to do with functions , So it is also called function scope .
1.4 JS There is no block-level scope
Block scope is defined by { } Include .
In other programming languages ( Such as java、c# etc. ), stay if sentence 、 A variable created in a loop statement , Only in Ben if sentence 、 Used in this loop statement , Like the one below Java Code :
java There are block-level scopes :
if(true){ int num = 123; system.out.print(num); // 123 } system.out.print(num); // Report errorsabove java The code will report an error , Because in the code { } That is, a scope , Where the declared variable num, stay “{ }” Cannot be used outside ;
And something like that JavaScript Code , You can't report a mistake :
Js There is no block-level scope in ( stay ES6 Before )
if(true){
var num = 123;
console.log(123); //123
}
console.log(123); //1232 - Scope of variable
stay JavaScript in , Depending on the scope , Variables can be divided into two types :
Global variables
local variable
2.1 Global variables
Variables declared under the global scope are called global variables ( Variables defined outside functions ).
Global variables can be used anywhere in the code
In global scope var Declared variables Global variable
Under special circumstances , Don't use... In functions var Declared variables are also global variables ( Not recommended )
2.2 local variable
Variables declared under local scope are called local variables ( A variable defined inside a function )
Local variables can only be used inside the function
Inside the function var The declared variable is a local variable
The formal parameters of a function are actually local variables
2.3 The difference between global and local variables
Global variables : It can be used anywhere , It will only be destroyed when the browser is closed , So it takes up more memory
local variable : Use only inside functions , When its code block is executed , Will be initialized ; After the contemporary code block runs , Will be destroyed , Therefore, it saves more memory space
3 - Scope chain
As long as the code is in a scope , The local scope written inside the function , It is not written inside any function, that is, in the global scope ; If there are functions in the function , Then a scope can be created in this scope ; Based on **[ Internal functions can access external function variables ]** This mechanism of , Use chain search to determine which data can be accessed by internal functions , It's called the scope chain case analysis 1:
function f1() {
var num = 123;
function f2() {
console.log( num );
}
f2();
}
var num = 456;
f1();
Scope chain : Take the principle of proximity to find the final value of the variable .
var a = 1;
function fn1() {
var a = 2;
var b = '22';
fn2();
function fn2() {
var a = 3;
fn3();
function fn3() {
var a = 4;
console.log(a); //a Value ?
console.log(b); //b Value ?
}
}
}
fn1();

边栏推荐
- [SQL injection] joint query (the simplest injection method)
- Thesis reading_ Tsinghua Ernie
- Market status and development prospect prediction of the global forward fluorescent microscope industry in 2022
- [tools run SQL blind note]
- Flutter monitors volume to realize waveform visualization of audio
- Online VR model display - 3D visual display solution
- Market status and development prospect prediction of global fermentation acid industry in 2022
- JDBC database operation
- Realize file download through the tag of < a > and customize the file name
- Without 50W bride price, my girlfriend was forcibly dragged away. What should I do
猜你喜欢

How to connect the network: Chapter 1 CSDN creation punch in

Gbase8s unique index and non unique index

ZABBIX monitoring of lamp architecture (2): ZABBIX basic operation

论文阅读_中文NLP_ELECTRA

Apache MPM model and ab stress test

Preparation for school and professional cognition

String matching: find a substring in a string

MediaTek 2023 IC written examination approved in advance (topic)

5-36v input automatic voltage rise and fall PD fast charging scheme drawing 30W low-cost chip

论文阅读_清华ERNIE
随机推荐
Chapter II program design of circular structure
Market status and development prospect prediction of the global autonomous hybrid underwater glider industry in 2022
Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
论文阅读_中文NLP_ELECTRA
1099 build a binary search tree (30 points)
Go language interface learning notes Continued
1103 integer factorization (30 points)
Force GCC to compile 32-bit programs on 64 bit platform
cookie session jwt
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)
Retirement plan fails, 64 year old programmer starts work again
JDBC database operation
Market status and development prospects of the global autonomous marine glider industry in 2022
1095 cars on campus (30 points)
How to connect the network: Chapter 1 CSDN creation punch in
MediaTek 2023 IC written examination approved in advance (topic)
Self introduction and objectives
"Hands on deep learning" pytorch edition Chapter II exercise
Learn to use the idea breakpoint debugging tool
并发操作-内存交互操作