当前位置:网站首页>js解析引擎执行js代码的步骤
js解析引擎执行js代码的步骤
2022-07-13 17:37:00 【frontEndSmallWhite】
浏览器包括渲染引擎和解析引擎,解析引擎主要负责解析js代码
一、语法分析
js解析引擎首先会先扫描所有的代码,查看代码是否存在语法错误,如果代码存在语法错误,程序立即报错,不会继续执行;如果未发现语法错误,则进入代码正式处理中的预解析(预编译)阶段;
二、代码正式处理
1、预编译阶段
全局执行代码:
①、创建全局对象GO,建立一个存放全局变量的仓库;
②、找到变量申明,提升变量申明,将变量存储在全局仓库中;
③、找到函数申明,提升函数申明;
js是单线程的语句,会解释一行执行一行,执行到函数时进入函数体,将控制权交给函数体;
函数内部代码:
①、创建AO对象;
②、找到变量声明和形参,赋值为默认值undefined;
③、将实参的值赋值给形参;
④、找到函数声明,然后提升函数声明;
代码实例:
<script>
function fn(num){
//首先创建AO,将变量声明和形参,赋值为默认值undefined;
// AO{
// num:undefined;
// num2:undefined;
// }
//然后将实参的值赋值给形参
// AO{
// num:5
// num2:undefined;
// }
//找到函数申明,提升函数申明
// AO{
// num:function num(){}
// num2:function(){console.log(1)}
// }
console.log(num); //function num(){}
var num = 10;
console.log(num); //10
function num(){} //函数申明,第三步提升
console.log(num); //10
console.log(num2); //function(){console.log(1)}
var num2 = function(){ //只有var num2;被提升,而且会被后面的函数申明覆盖
console.log(2)
}
console.log(num2); //function(){console.log(2)}
function num2(){ //函数申明,第三步提升(会覆盖掉前面的变量申明(num2)提升)
console.log(1)
}
}
fn(5);
</script>当函数执行完毕之后,函数会被销毁;
边栏推荐
- 38.js--原型练习案例(校招面试题)
- Records of several classical power spectral density estimation methods (from the perspective of filtering)
- ES6--类(class)
- ES6--对象
- ES6--Set
- Register the implementation.
- C语言:有关字符串函数的一些自我实现
- What if the system always fails? Maybe you should learn about stability construction
- Use of command line client
- Random signal analysis, 2nd Edition [edited by Zhao Shuqing and Zheng Wei] (part) answer to homework after class (written by myself)
猜你喜欢

完整在pycharm上通过torch调用GPU(最细节)

【黄啊码】老师,高考后我想选软件开发相关专业,你觉得选哪个方向好?高考志愿怎么填报?

Summary of wechat red envelope project test

【黄啊码】MySQL入门—2、使用数据定义语言(DDL)操作数据库

41.js--闭包
Dark blue themed boss, can you add a typewriter function so that you can choose when typing

37.js--对象的成员操作和原型对象的操作(主要是相关程序举例)

【一知半解】AQS

Blue Bridge Cup: the second game of the 13th provincial competition of single chip microcomputer group

【黄啊码】MySQL的这些小知识点,初入门的你必须得学会
随机推荐
41.js--闭包
Oracle数据库 错误代码解决办法
Summary of wechat red envelope project test
<statement> or DELIMITER expected, got ‘id‘
MVN clean or MVN clean package did not detect the test file
ES5中数组对象有哪些方法和ES6新增了哪些方法
JS数组的内置功能
遍历器总结
扩展知识——JS的劫持技术
藍橋杯單片機第13届省賽題
Keil reports an error: error 118 (errors reference to external variables) solution and reason
JS字符串内置功能
油猴脚本更改tw樣式
Kubernetes into the pit
Why blog?
js复制文本到剪切板
第一次放生,感觉很奇妙
ES6--let与const
sql_server2014下载与安装
C语言字符和字符串