当前位置:网站首页>js引擎运行中的预解析(变量提升和函数提升)及相关实操案例
js引擎运行中的预解析(变量提升和函数提升)及相关实操案例
2022-08-02 08:22:00 【前端开发boy】
js引擎运行js分为两步:预解析 代码执行
(1) 预解析js引擎会把js里面所有的var ,还有function提升到当前作用域的最前面
(2) 代码执行 按照代码书写的顺序从上往下执行
预解析分为 变量预解析(变量提升)和 函数预解析(函数提升)
(1) 变量提升 就是把所有的变量声明提升到当前作用域最前面,不提升赋值操作
实例1:console.log(sum); //undefined var sum;
===> 转换为 console.log(sum);
var sum = 10; sum = 10;
实例2:fun(); // 报错 var fun;
var fun = function() { fun();
console.log(22); ===> 转换为 fun = function() {
} console.log(22);
}
(2) 函数提升 就是把所有的函数声明提升到当前作用域最前面,不调用函数
实例: fn(); // 11 function fn() {
function fn() { console.log(11);
console.log(11); ===> 转换为 }
} fn(); // 11
预解析案例:
案例1 结果是?undefined
var num = 10;
fun();
function fun() {
console.log(num);
var num = 20;
}
<script>
// 上面案例预解析为下面的代码
var num;
function fun() {
var num;
console.log(num);
num = 20;
}
num = 10;
fun(); // undefined
</script>
案例2 结果是?
var num = 10;
function fn() {
console.log(num);
var num = 20;
console.log(num);
}
fn();
<script>
// 上面案例预解析为下面的代码
var num;
function fn() {
var num;
console.log(num); // undefined
num = 20;
console.log(num); // 20
}
num = 10;
fn();
</script>
案例3 结果是?
var a = 18;
f1();
function f1() {
var b = 9;
console.log(a); // undefined
console.log(b); // 9
var a = '123';
}
<script>
// 上面案例预解析为下面的代码
var a;
function f1() {
var b;
var a;
b = 9;
console.log(a); // undefined
console.log(b); // 9
a = '123';
}
a = 18;
f1();
</script>
案例4 结果是?
f1();
console.log(c); // 9
console.log(b); // 9
console.log(a); // 报错
function f1() {
var a = b = c = 9;
console.log(a); // 9
console.log(b); // 9
console.log(c); // 9
}
<script>
// 上面案例预解析为下面的代码
function f1() {
var a;
a = b = c = 9;
// 相当于 var a = 9; b = 9; c = 9; b 和 c 直接赋值,没有var 声明当全局变量看
console.log(a); // 9
console.log(b); // 9
console.log(c); // 9
}
f1();
console.log(c); // 9
console.log(b); // 9
console.log(a); // 报错
</script>
边栏推荐
- Flink 监控指南 被动拉取 Rest API
- 主流监控系统工具选型及落地场景参考
- unity pdg 设置隐藏不需要的节点以及实现自动勾选自动加载项
- OneinStack多版本PHP共存
- LeetCode_2358_分组的最大数量
- Biotin-EDA|CAS:111790-37-5| Ethylenediamine biotin
- BGP solves routing black hole through MPLS
- High imitation [Huawei consumer business official website] and wonderful animation analysis: practice embedding JS code in low-code platform
- 十大免费cms建站系统介绍推荐
- C语言_指针
猜你喜欢
(Note) AXIS ACASIS DT-3608 Dual-bay Hard Disk Array Box RAID Setting
PyQt5(一) PyQt5安装及配置,从文件夹读取图片并显示,模拟生成素描图像
抓包工具Charles修改Response步骤
EPSANet: An Efficient Pyramid Split Attention Block on Convolutional Neural Network
A young man with strong blood and energy actually became a housekeeper. How did he successfully turn around and change careers?
C语言基础_结构体
RestTemlate源码分析及工具类设计
Business Intelligence Platform BI Business Intelligence Analysis Platform How to Choose the Right Business Intelligence Platform BI
Pycharm (1) the basic use of tutorial
PyCharm usage tutorial (detailed version - graphic and text combination)
随机推荐
Detailed explanation of calculation commands in shell (expr, (()), $[], let, bc )
AI目标分割能力,无需绿幕即可实现快速视频抠图
ORBSLAM代码阅读
轴流式水轮机隐私政策
BGP solves routing black hole through MPLS
Redis分布式锁入门
商业智能平台BI 商业智能分析平台 如何选择合适的商业智能平台BI
What is NoSQL?Databases for the cloud-scale future
day_05模块
R language plotly visualization: plotly visualizes the scatter plot of the actual value of the regression model and the predicted value of the regression, analyzes the prediction performance of the re
自定义View实现波浪荡漾效果
Axial Turbine Privacy Policy
十大免费cms建站系统介绍推荐
Ansible learning summary (11) - detailed explanation of forks and serial parameters of task parallel execution
The crawler video crawl tools you get
MySQL 中 count() 和 count(1) 有什么区别?哪个性能最好?
三维体尺测量
pnpm: Introduction
Biotinyl Cystamine | CAS: 128915-82-2 | biotin cysteamine
UVM事务级建模