当前位置:网站首页>js中的变量提升和函数提升
js中的变量提升和函数提升
2022-07-04 12:47:00 【51CTO】
js中的变量提升和函数提升
打印的值第一个为undefined,而第二个打印的值为yyyyy.
实际的方法内部代码执行顺序应该是这样的:
看到拆分后的代码执行顺序对结果也就不迷茫了。
为什么有变量提升
那么为什么会出现变量提升这个现象呢?
其实js和其他语言一样,都要经历编译和执行阶段。而js在编译阶段的时候,会搜集所有的变量声明并且提前声明变量,而其他的语句都不会改变他们的顺序,因此,在编译阶段的时候,第一步就已经执行了,而第二步则是在执行阶段执行到该语句的时候才执行。
总结
1.js会将变量的声明提升到js顶部执行,因此对于这种语句:var a = 2;其实上js会将其分为var a;和a = 2;两部分,并且将var a这一步提升到顶部执行。
2.变量提升的本质其实是由于js引擎在编译的时候,就将所有的变量声明了,因此在执行的时候,所有的变量都已经完成声明。
3.当有多个同名变量声明的时候,函数声明会覆盖其他的声明。如果有多个函数声明,则是由最后的一个函数声明覆盖之前所有的声明。
边栏推荐
- Dgraph: large scale dynamic graph dataset
- Agile development / agile testing experience
- Getting started with microservices
- Optional values and functions of the itemized contenttype parameter in the request header
- Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
- C#基础补充
- After the game starts, you will be prompted to install HMS core. Click Cancel, and you will not be prompted to install HMS core again (initialization failure returns 907135003)
- 2022KDD预讲 | 11位一作学者带你提前解锁优秀论文
- Go 语言入门很简单:Go 实现凯撒密码
- 上汽大通MAXUS正式发布全新品牌“MIFA”,旗舰产品MIFA 9正式亮相!
猜你喜欢
洞见科技解决方案总监薛婧:联邦学习助力数据要素安全流通
动画与过渡效果
AI painting minimalist tutorial
Talk about the design and implementation logic of payment process
unity不识别rider的其中一种解决方法
.Net之延迟队列
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
Is the outdoor LED screen waterproof?
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
随机推荐
《预训练周刊》第52期:屏蔽视觉预训练、目标导向对话
ASP.NET Core入门一
"Tips" to slim down Seurat objects
Building intelligent gray-scale data system from 0 to 1: Taking vivo game center as an example
字节面试算法题
Cors: standard scheme of cross domain resource request
分布式BASE理论
.Net之延迟队列
C foundation in-depth learning II
The old-fashioned synchronized lock optimization will make it clear to you at once!
The only core indicator of high-quality software architecture
MySQL three-level distribution agent relationship storage
When MDK uses precompiler in header file, ifdef is invalid
室外LED屏幕防水吗?
It is six orders of magnitude faster than the quantum chemical method. An adiabatic artificial neural network method based on adiabatic state can accelerate the simulation of dual nitrogen benzene der
洞见科技解决方案总监薛婧:联邦学习助力数据要素安全流通
unity不识别rider的其中一种解决方法
Golang sets the small details of goproxy proxy proxy, which is applicable to go module download timeout and Alibaba cloud image go module download timeout
求解:在oracle中如何用一条语句用delete删除两个表中jack的信息
Oracle 被 Ventana Research 评为数字创新奖总冠军