当前位置:网站首页>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.当有多个同名变量声明的时候,函数声明会覆盖其他的声明。如果有多个函数声明,则是由最后的一个函数声明覆盖之前所有的声明。
边栏推荐
- Iptables foundation and Samba configuration examples
- C语言程序设计选题参考
- Comprehensive evaluation of modular note taking software: craft, notation, flowus
- 洞见科技解决方案总监薛婧:联邦学习助力数据要素安全流通
- CTF competition problem solution STM32 reverse introduction
- unity不识别rider的其中一种解决方法
- SQL statement syntax error in test SQL statement deletion in eclipse linked database
- Flet tutorial 03 basic introduction to filledbutton (tutorial includes source code) (tutorial includes source code)
- JVM系列——栈与堆、方法区day1-2
- 嵌入式编程中五个必探的“潜在错误”
猜你喜欢

ASP. Net core introduction I

数据库公共字段自动填充

2022年中国移动阅读市场年度综合分析

JVM series - stack and heap, method area day1-2

2022kdd pre lecture | 11 first-class scholars take you to unlock excellent papers in advance

Flet教程之 03 FilledButton基础入门(教程含源码)(教程含源码)

源码编译安装MySQL

A data person understands and deepens the domain model

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

嵌入式编程中五个必探的“潜在错误”
随机推荐
XML入门二
Xilinx/system-controller-c/boardui/ unable to connect to the development board, the solution of jamming after arbitrary operation
Runc hang causes the kubernetes node notready
C#基础深入学习一
聊聊支付流程的设计与实现逻辑
字节面试算法题
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
The only core indicator of high-quality software architecture
C语言职工管理系统
When MDK uses precompiler in header file, ifdef is invalid
N++ is not reliable
再说rsync+inotify实现数据的实时备份
C foundation in-depth study I
C语言集合运算
求解:在oracle中如何用一条语句用delete删除两个表中jack的信息
Haproxy high availability solution
Using nsproxy to forward messages
2022年中国移动阅读市场年度综合分析
《预训练周刊》第52期:屏蔽视觉预训练、目标导向对话
CVPR 2022 | transfusion: Lidar camera fusion for 3D target detection with transformer