当前位置:网站首页>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();

边栏推荐
- Class loading mechanism (detailed explanation of the whole process)
- Market status and development prospect prediction of the global fire hose industry in 2022
- Analysis of proxy usage of ES6 new feature
- M1 Pro install redis
- Do you know UVs in modeling?
- First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
- 1095 cars on campus (30 points)
- 1115 counting nodes in a BST (30 points)
- MySQL master-slave configuration
- Thesis reading_ Chinese NLP_ ELECTRA
猜你喜欢
![[XSS bypass - protection strategy] understand the protection strategy and better bypass](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[XSS bypass - protection strategy] understand the protection strategy and better bypass

Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)

Silent authorization login and registration of wechat applet
![[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree](/img/0f/bc8c44aee7a2c9dccac050b1060017.jpg)
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree

Basic knowledge of reflection (detailed explanation)

Celebrate the new year together

JS dynamic table creation

Basic use of Metasploit penetration testing framework

论文阅读_中文医疗模型_ eHealth

Preparation for school and professional cognition
随机推荐
50 practical applications of R language (36) - data visualization from basic to advanced
【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)
Review the configuration of vscode to develop golang
Retirement plan fails, 64 year old programmer starts work again
动态规划——相关概念,(数塔问题)
Blog building tool recommendation (text book delivery)
sql语句模糊查询遇到的问题
Oracle SQL table data loss
Thesis reading_ Tsinghua Ernie
[research materials] 2021 China's game industry brand report - Download attached
Basic use of Metasploit penetration testing framework
Current market situation and development prospect prediction of global direct energy deposition 3D printer industry in 2022
My first Smartphone
Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)
Market status and development prospect prediction of global fermented plant protein industry in 2022
How to connect the network: Chapter 1 CSDN creation punch in
Market status and development prospect prediction of the near infrared sensor industry of the global Internet of things in 2022
Basic knowledge of reflection (detailed explanation)
Prepare for 2022 and welcome the "golden three silver four". The "summary of Android intermediate and advanced interview questions in 2022" is fresh, so that your big factory interview can go smoothly
[SQL injection] joint query (the simplest injection method)