当前位置:网站首页>预解析案例
预解析案例
2022-08-06 15:12:00 【恬淡如雪】
1.js引擎运行js分为两步:预解析和代码执行
(1)预解析js引擎会把js里面所有的 var 和 function 提升到当前作用域的最前面;
(2)代码执行 按照代码书写的顺序从上往下执行。
2. 预解析分为 变量预解析(变量提升)和函数预解析(函数提升)
(1)变量提升:就是把所有的变量声明提升到当前的作用域最前面,不提升赋值操作;
(2)函数提升:就是把所有的函数声明提升到当前作用域的最前面 ,不调用函数。
案例一
var num = 10;
fun();
function fun() {
console.log(num); //underfine
var num = 20;
}
// 相当于执行以下代码
var num;
function fun() {
var num;
console.log(num);
num = 20
}
num = 10;
fun();
案例二
var a = 18;
f1();
function f1() {
var b = 9;
console.log(a); //underfine
console.log(b); //9
var a = '123';
}
// 相当于执行以下代码
var a;
function f1() {
var b;
var a;
b = 9;
console.log(a);
console.log(b);
a = '123';
}
a = 18;
f1();
案例三
f1();
console.log(c);
console.log(b);
console.log(a);
function f1() {
var a = b = c = 9;
console.log(a);
console.log(b);
console.log(c);
}
// 相当于执行以下代码
function f1() {
var a = b = c = 9;
// 相当于 var a=9; b=9; c=9; b 和 c 直接赋值 没有var声明 当 全局变量
// 集体声明 var a=9, b=9, c=9
console.log(a); //9
console.log(b); //9
console.log(c); //9
}
f1();
console.log(c); //9
console.log(b); //9
console.log(a); //报错
案例四
function b() {
a = 10;
return;
}
var a = 1;
b();
console.log(a); //10
// 相当于执行
function b(){
a=10; //全局变量
return;
}
var a;
a=1;
b();
console.log(a); //10
//因为先执行a=1,后面再执行函数,由于函数里面的a是全局变量,函数里面没有a的地址,就会
//往外面找,结果找到了a的地址,然后再把a=10 赋值给a
案例五
var a = 1;
function b() {
a = 10;
return;
//a函数声明,提前变量a,将a认为是函数b作用域的变量,具有局部效果
function a() { }
}
b();
console.log(a);
// 相当于执行 function b()相等于var b=function()
var a;
var b;
b = function () {
var a;
a = 10;
return;
a = function () { }
}
边栏推荐
- 网上开户佣金万一安全吗?手机办理流程是怎样的
- Engineers invented about 48 hours, millet millet head hoop, netizen: change the Monkey King is not a dream!
- 【Paper Speed Reading】NLNL: Negative Learning for Noisy Labels (ICCV2019)
- 豪威宣布发布世界首款产品级 CIS / EVS 融合视觉芯片
- 实用新型专利申请文件撰写示例
- MySQL数据库成为瓶颈后,动态数据的查询要如何加速?
- stc8a--al422B————01, RE has been grounded on the hardware.
- 口碑极佳的7个公众号!
- Come and watch | How do the big guys deal with the risk control feature variable pool
- 【安装填坑】-import win32api, sys, os ImportError: DLL load failed: 找不到指定的模块。
猜你喜欢

实用新型专利申请文件撰写示例

RL-D1电流继电器

LeetCode Brushing Diary: 899. Ordered Queue

【安装填坑】-import win32api, sys, os ImportError: DLL load failed: 找不到指定的模块。

Coggle 30 Days of ML【打卡】广告-信息流跨域ctr预估

00后写个暑假作业,被监控成这笔样

SAP BAPI 教程 – 在 ABAP 中创建 BAPI 的分步指南-020

博云入选 Gartner 中国云原生领域代表性厂商

New kernel PHP enterprise website development and construction management system

ES core concepts
随机推荐
【leetcode周赛总结】
1322_Information sorting of queue usage in FreeRTOS and use of preliminary queues
用于毫米波雷达的GNN:Radar-PointGNN
小程序跳转方式
Kotlin 协程之取消与异常处理探索之旅(上)
Field userService in com.zher.reggie_task_out.controller.UserController required a bean of type ‘com
cmd命令行工具
【LeetCode】658. Find the K closest numbers
value to 0 operation
有重复字符的排列组合[回溯 & 剪枝去重 || set去重]
【paper速读】NLNL: Negative Learning for Noisy Labels (ICCV2019)
JGL-15/5反时限过流继电器
Apache Calcite入门
爬虫之Scrapy框架
重构指标之如何监控代码圈复杂度
car audio service详解
【LeetCode】658.找到K个最接近的数
一个案例搞懂工厂模式和单例模式
如何使用xilinx的DDS生成多项数据
作者简介&系列文章