当前位置:网站首页>Variables, scopes, and variable promotion
Variables, scopes, and variable promotion
2022-06-25 14:12:00 【Rabbit^-^】
Variable 、 Scope and memory issues
Global variables : Global variable definitions can be provided to all code blocks and function calls
if(true){
var c=33
}console.log(c)//33 Access to
Private variables : stay js Next , Only the variables defined in the function are private
var a=11
function fun(){
var b=12
console.log(a)//11
console.log(b)//12
}
fun()
console.log(a)//11
console.log(b)// Report errors , Cannot call ,b It's a private variable Basic data type :null undefined number string boolean
Reference data type :object

Variable promotion and scope
Pre explanation ( Variable Promotion )
In the current scope ,js Before code execution , The browser will first bring var and function Contents of the declaration , Make a declaration or definition in advance
Difference between declaration and definition
var a // Statement : Tell the browser to store a variable in memory
var b=11// Definition : Not only defined variables , Assignment is also made
The difference between the function of variable promotion and variable
var Only the declaration is completed in memory
function The memory is defined and declared , Only when the function is not called, it stores a string
for example :
Console.log(a)// Output undefined
Var a=11
fun()// Execute function
function fun(){
Console.log(“ This is a function ”)
}
Precautions for variable Promotion
1、 Does not define var The difference between
console.log(a)//undefined
console.log(b)// Report errors , There is no process of variable Promotion
var a=11
b=22
2、 Whether or not the conditions are valid , We need to upgrade the variables
console.log(a)// Output undefined Variables exist
if(false){
var a=12
}
- When executing an anonymous function , There is no pre explanation , The execution of the code is done together with the definition
(function(){
var a=b=3 //var b=3 a=b
})()
console.log(b)//3 Make a statement first b b The variable goes up
console.log(a)// Report errors a No domain interpretation a No variable Promotion - When... In a function return Statement execution time , Although the following statement does not execute , But it also requires variable elevation
Be careful : Function scope , There are private variables. Find private variables , No formal parameters found , No formal parameters , Find the whole situation , If the formal parameters and private variables are promoted , You won't find global variables , Output undefined
function fun(){
console.log(a)
return false
var a=66
}
fun()- stay js in , The variable and function names are duplicated , There will also be conflict , When the variable is raised , If the name is declared , Will not declare again , But it can be reassigned
function num(){
console.log(111)
}
num()
var num=66
console.log(num)// Output :66;var Will not restate , But you can change the value
num()// Report errors 边栏推荐
- 程序員為什麼要軟一點?
- Realization of neural networks with numpy
- 多台云服务器的 Kubernetes 集群搭建
- Today in history: Netease was founded; The first consumer electronics exhibition was held; The first webcast in the world
- 一次性总结:64个数据分析常用术语!
- 哈希錶、哈希沖突
- Classifier and cross entropy loss function
- Shell operator
- Experts' suggestions | 8 measures to accelerate your innovative career planning and growth
- 触觉智能分享-RK3568在金融自助终端的应用
猜你喜欢
![[open source Hongmeng system display] the rk3568 development board is equipped with openharmony 3.1 release](/img/c4/0bfb380d38b5205a02a10175ee6888.png)
[open source Hongmeng system display] the rk3568 development board is equipped with openharmony 3.1 release

Network remote access using raspberry pie

【Proteus仿真】51单片机+DS1302+lcd1602显示

Page 112 machine learning - review of fundamentals of mathematics pptx

shell 字符串变量

As a software testing engineer, how do you think to ensure software quality?

Gorm---- Association query

Numpy库使用入门

NR-ARFCN和信道栅格、同步栅格和GSCN

Rust,程序員創業的最佳選擇?
随机推荐
As a software testing engineer, how do you think to ensure software quality?
Why should programmers be softer?
112页机器学习-数学基础回顾.pptx
删库跑路、“投毒”、改协议,开源有哪几大红线千万不能踩?
Vscode--- format setting configuration
戴尔电脑cpu温度过高怎么办
shell 运算符
k线图24种经典图解(影线篇)
Discriminative v.s.Generative
What are the red lines of open source that should not be trodden on?
Nr-arfcn and channel grid, synchronous grid and GSCN
None of the MLIR optimization passes are enabled (registered 2) solutions
Websocket -- reverse proxy to solve cross domain problems
About reconnection of STM32 using lan8720a plug-in network cable
[world history] Episode II: Dawn of civilization
JVM uses tools to analyze classic cases of OOM
Graph contractual learning with augmentations
Explain the possible memory leaks caused by the handler at one time and the solutions | the developer said · dtalk
触觉智能分享-RK3568在金融自助终端的应用
K-line diagram 24 classic diagrams (shadow)