当前位置:网站首页>JS variable points you don't know
JS variable points you don't know
2022-07-18 16:10:00 【Lu Rongtao】
stay JavaScript Explicitly declare... In Variables and implicitly declared variables have some points that we need to pay attention to
notes : Not for the moment ES6 Medium let and const
Use var A variable declared by a keyword is an explicit variable declaration
Such as :var abc1 = ‘hello’
js It is allowed not to use var To declare variables is to declare variables implicitly
Such as :abc2 = ‘world’
that , The problem is coming. , Is there any difference between these two variables ?
If abc1 and abc2 All in the global scope , Print on the browser console window object :
console.log( window )
Here's the picture :
As you can see in the picture above :
Variables in the global abc1 and abc2 All are window Object properties , All have global scope .
If abc1 and abc2 It's all in the function scope , Print on the browser console window object ?
function test(){
var abc1 = ‘hello’
abc2 = ‘world’
}
test()
console.log( window )
Here's the picture :
As you can see in the picture above :
In the scope of a function abc1 Will not appear in window In the object , Is a local variable ; and abc2 Is still window Object properties , Has global scope .
If abc1 and abc2 All in the global scope , All are window Object properties , Can they be deleted just like object attributes ?
var abc1 = ‘hello’
abc2 = ‘world’
delete window.abc1
delete window.abc2
console.log( window )
Here's the picture :
As you can see in the picture above :
Use var Declared variables abc1 Not deleted , Is still window Object properties ; Instead of using var Declared variables abc2 It has been deleted .
Why is there such a difference ?
This is related to the default object property descriptor !
Object.getOwnPropertyDescriptor(obj, prop) // Returns the property descriptor of the object
var abc1 = ‘hello’ // Declare variables explicitly
var props1 = Object.getOwnPropertyDescriptor(window,‘abc1’)
console.log( props1 )
Here's the picture :
As you can see in the picture above :
window Object properties abc1 In the descriptor of ,configurable: false Indicates that the property is not configurable 、 Not delete .
abc2 = ‘world’ // Implicitly declare variables
var props2 = Object.getOwnPropertyDescriptor(window,‘abc2’)
console.log( props2 )
Here's the picture :
As you can see in the picture above :
window Object properties abc2 In the descriptor of ,configurable: true Indicates that the property is configurable 、 Deleting .
The default settings in the object property descriptor are different , It leads to the difference of whether it can be deleted !
Besides , There are also differences between these two forms of variables in parsing execution :
console.log( abc1 )
var abc1 = ‘hello’
Here's the picture :
As you can see in the picture above :
Print first abc1 And then declare variables abc1, Results comparison ‘ Weird ’ by undefined, Instead of reporting a mistake !
stay js Variable declaration and function declaration appear in ‘ Declaration upgrade ’,js The engine parsing execution code is divided into two stages :1. Pre parsing stage ;2. Line by line execution phase .
In the pre parsing phase, variables abc1 Is declared and assigned an initial value of undefined;
In the line by line execution phase, variables abc1 To be an assignment ‘hello’;
therefore , Printing before variable declaration does not report an error , The value printed is undefined.
console.log( abc2 )
abc2 = ‘world’
Here's the picture :
abc2 Undefined , Similar operation , The fate is totally different !
Don't use var Declared variables , No, ‘ Declaration upgrade ’ The mechanism of , That is, the variables in the pre parsing phase are not declared , It's only after the line by line execution phase is assigned a value that there are variables abc2, So print first abc2 You're going to report a mistake !
* Lu Rongtao front-end learning exchange Q Group 858752519
Add group notes :CSDN recommend
边栏推荐
- leetcode刷题记录17
- 精度提升方法:自适应Tokens的高效视觉Transformer框架(已开源)
- query string、formData和request payload的区别
- JS变量你不知道的点
- CF609A USB Flash Drives
- Oracle存储过程的几种调用方式
- Autojs learning - sound transformer template
- NFT玩家的共识分片:金钱、社区与文化
- 1388.3n pizza dynamic planning
- Joint autoregressive and hierarchical priorities for learned image compression
猜你喜欢

Fosai biology interprets the changes in the atmospheric environment in the first half of 2022, and VOCs control is still the key to breaking the situation

实现团队工作效率与质量共赢,这款办公协同工具真够用!

Playing with "private e-commerce", is the chain 2+1 model worth enterprises' in-depth understanding?

Web page making (II)

玩转“私域电商”,链动2+1模式值得企业深入了解吗?

Kubedm install kubernetes 1.15 best practices

学习笔记——直流电机调速器

465-剑指offer(53-I、53-II、04、50)

ReFi夏季升温:Uniswap v3和绿色资产池在Celo上启动

FPGA 20 routines: 8 Reading and writing of any address of SD card
随机推荐
CF514B Han Solo and Lazer Gun
C语言实训通讯录(静态和动态版本)
中关村e谷·苏高新承办2022苏州中日韩高层次人才项目路演大赛
464-剑指offer(35、05、58、03)
uniapp扫码原生插件(Google MLKit、zxing;支持同时扫多个码)
全面解析NFT的流动性问题与解决方案
Web page making (II)
JS变量你不知道的点
LCA problem topic
以Celsius为反面教材,手把手教大家判断产品好坏、避开投资风险
Deepin wine QQ/微信中文显示为方块的原因之一
Start with a notice to prevent phishing emails
How apisik integrates with Hydra to build a centralized authentication gateway to help enterprise security
Uniapp code scanning native plug-ins (Google mlkit, zxing; support scanning multiple codes at the same time)
465-剑指offer(53-I、53-II、04、50)
Talk about promise
What does SPK interface mean
RSS上手教程:聚合自己的信息收集渠道,RSSHub、FreshRSS、NetNewsWire
CF514B Han Solo and Lazer Gun
Elk cluster startup sequence of elk cluster deployment (7)