当前位置:网站首页>JS预解析
JS预解析
2022-06-12 06:03:00 【来自万古的忧伤】
JavaScript 代码是由浏览器中的 JavaScript 解析器来执行的。JavaScript 解析器在运行 JavaScript 代码的时候分为两步:预解析和代码执行。
预解析:在当前作用域下, JS 代码执行之前,浏览器会默认把带有 var 和 function 声明的变量在内存中进行提前声明或者定义。代码执行: 从上到下执行JS语句。预解析只会发生在通过 var 定义的变量和 function 上。
学习预解析能够让我们知道为什么在变量声明之前访问变量的值是 undefined,为什么在函数声明之前就可以调用函数。
预解析也叫做变量、函数提升。
变量提升: 变量的声明会被提升到当前作用域的最上面,变量的赋值不会提升。
函数提升: 函数的声明会被提升到当前作用域的最上面,但是不会调用函数。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script> // 1问 console.log(num); // 2问 console.log(num); // undefined 坑 1 var num = 10; // 相当于执行了以下代码 // var num; // console.log(num); // num = 10; // 3问 function fn() {
console.log(11); } fn(); // 4问 fun(); // 报错 坑2 var fun = function() {
console.log(22); } // 函数表达式 调用必须写在函数表达式的下面 // 相当于执行了以下代码 // var fun; // fun(); // fun = function() {
// console.log(22); // } // 1. 我们js引擎运行js 分为两步: 预解析 代码执行 // (1). 预解析 js引擎会把js 里面所有的 var 还有 function 提升到当前作用域的最前面 // (2). 代码执行 按照代码书写的顺序从上往下执行 // 2. 预解析分为 变量预解析(变量提升) 和 函数预解析(函数提升) // (1) 变量提升 就是把所有的变量声明提升到当前的作用域最前面 不提升赋值操作 // (2) 函数提升 就是把所有的函数声明提升到当前作用域的最前面 不调用函数 </script>
</head>
<body>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script> // 预解析案例 // 案例1 // var num = 10; // fun(); // function fun() {
// console.log(num); // var num = 20; // } // // 相当于执行了以下操作 // // var num; // // function fun() {
// // var num; // // console.log(num); // // num = 20; // // } // // num = 10; // // fun(); // // 案例2 // var num = 10; // function fn() {
// console.log(num); // var num = 20; // console.log(num); // } // fn(); // // 相当于以下代码 // // var num; // // function fn() {
// // var num; // // console.log(num); // // num = 20; // // console.log(num); // // } // // num = 10; // // fn(); // // 案例3 // var a = 18; // f1(); // function f1() {
// var b = 9; // console.log(a); // console.log(b); // var a = '123'; // } // 相当于以下代码 // var a; // function f1() {
// var b; // var a; // b = 9; // console.log(a); // console.log(b); // a = '123'; // } // a = 18; // f1(); // 案例4 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; // a = b = c = 9; // // 相当于 var a = 9; b = 9; c = 9; b 和 c 直接赋值 没有var 声明 当 全局变量看 // // 集体声明 var a = 9, b = 9, c = 9; // console.log(a); // console.log(b); // console.log(c); // } // f1(); // console.log(c); // console.log(b); // console.log(a); </script>
</head>
<body>
</body>
</html>
边栏推荐
- MySQL 主从,6 分钟带你掌握
- Leetcode-1604. Warning people who use the same employee card more than or equal to three times within one hour
- The application could not be installed: INSTALL_ FAILED_ TEST_ ONLY
- Execute sh script to prompt "[[: not found" solution. The difference between Bash and sh
- Leetcode sword finger offer II 033 Modified phrase
- Leetcode 第 80 场双周赛题解
- Why don't databases use hash tables?
- How do I get the date and time from the Internet- How to get DateTime from the internet?
- Market trend report, technical innovation and market forecast of Chinese stump crusher
- Leetcode-1535. Find the winner of the array game
猜你喜欢

Understanding of distributed transactions

dlib 人脸检测

数据库为什么不使用hash表?

Simple spiral ladder generation for Houdini program modeling

Front desk display LED number (number type on calculator)

Houdini terrain creation

Leetcode simple problem: converting an integer to the sum of two zero free integers

Directx11 advanced tutorial tiled based deffered shading

Tabulation skills and matrix processing skills

Why is the union index the leftmost matching principle?
随机推荐
Liunx Foundation
March 23, 2021
IDEA常用配置
Getting started with houdininengine HDA and UE4
R语言大作业(四):上海市、东京 1997-2018 年GDP值分析
交叉编译libev
Introduction to thread pool: ThreadPoolExecutor
Recursive implementation of exponential, permutation and combination enumerations
IO to IO multiplexing from traditional network
Multiple ways 99.9% to solve the problem of garbled code after copying text from PDF
Houdini script vex learning
Heap classical problem
[untitled]
Flutter monitors application lifecycle status
EBook upload
Database experiment I: data definition experiment guide
[C language basics] macro definition usage
468. verifying the IP address
Login authentication filter
The application could not be installed: INSTALL_ FAILED_ TEST_ ONLY