当前位置:网站首页>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 边栏推荐
- Suanli & NFT trading platform f3 The exclusive NFT project of XYZ, hash eagle, will be grandly launched
- Gorm-- search you don't know
- 使用KVM虚拟化部署EVE-NG
- Dialogue: recommended system quick start route and summary of knowledge points
- Realization of neural networks with numpy
- 【世界历史】第一集——石器时代的人们
- Page 112 machine learning - review of fundamentals of mathematics pptx
- Use of bind() in JS and handwritten bind()
- 测一测你的挣钱能力有多强?未来的你将从事什么职业?
- 网络远程访问的方式使用树莓派
猜你喜欢

中国电池技术取得重大突破,日韩美都落后了,中国巩固了领先优势

Numpy库使用入门

用NumPy实现神经网络(Mysteries of Neural Networks Part III)

Suanli & NFT trading platform f3 The exclusive NFT project of XYZ, hash eagle, will be grandly launched

数据采集系统网关采集工厂效率

shell 变量 入门

Getting started with shell variables

Kubernetes cluster construction of multiple ECS

Where can the brightness of win7 display screen be adjusted

Solving error: creating window glfw error: glew initialization error: missing GL version
随机推荐
深入理解深度神经网络背后的数学(Mysteries of Neural Networks Part I)
Class usage and inheritance in ES6
JVM 用工具分析OOM经典案例
golang项目依赖管理工具go vendor,go mod
shell 内置命令
How does hash eagle, the founder of equity NFT, redefine NFT and use equity to enable long-term value?
作为一名软件测试工程师你认为怎么样才能保证软件质量?
Getting started with shell variables
Is it safe to open an account with tongdaxin stock?
Classifier and cross entropy loss function
英语中的九大词性与九大时态
【世界历史】第二集——文明的曙光
英語中的九大詞性與九大時態
[world history] Episode II: Dawn of civilization
Mysql database compressed backup_ Summary of MySQL backup compression and database recovery methods
Dialogue: recommended system quick start route and summary of knowledge points
Renix Perf: IP网络性能测试工具及测试用例参数详解
当了六年程序员第一次搞懂微服务架构的数据一致性,真不容易
Windows下MySQL的安装和删除
使用调试工具调试博图TCP连接所遇到的问题