JavaScript-预解析(变量提升)

crystal_iwwish 2021-11-25 18:44:50
javascript 变量 解析 提升 javascript-

代码在执行之前先要预解析

不是所有东西都要解析,主要解析变量和函数,只有带有声明的变量(n=7 没有声明),带有名字的函数才会被解析

总结:1. 变量在未声明即被访问时会报语法错误
2. 变量在声明之前即被访问,变量的值为 undefined
3. let 声明的变量不存在变量提升,推荐使用 let【也有人认为具有提升但是不赋值不能使用】
4. 变量提升出现在相同作用域当中
5. 实际开发中推荐先声明再访问变量

变量预解析:把变量的声明语法提升到作用域最前面(只定义不赋值)——变量提升

直接赋值的变量不会预解析(没有加声明的全局变量)

var n = 1;
console.log(n);
//var n;---解析出来的
console.log(n);
var n = 1;

函数预解析:把函数的声明语法提升到作用域最前面(只声明不调用)——函数提升

//var a 预解析
//function f1 () {

// var b
// var b = 9;
// console.log(a);//undifined
// console.log(b);//9
// var a = 123;
}
var a = 18;
f1();
function f1 () {

var b = 9;
console.log(a);
console.log(b);
var a = 123;
}
  1. 函数提升能够使函数的声明调用更灵活
  2. 函数表达式不存在提升的现象
  3. 函数提升出现在相同作用域当中

let 预解析

 function fn() {

console.log(n);
let n = 123; //报错,let 不存在预解析,必须先定义后使用,
}
fn();

例子1:
▲▲▲函数优于变量

//function a () {

console.log("aa");
}
//var a
console.log(a)//函数
function a () {

console.log("aa");
}s
var a = 1;
console.log(a);//1

例子2:

注意:var a = b = c = 9; 这句话等价于一下三句:
var a = 9;
b = 9;
c = 9;
//function f1 () {

//var a
var a = b = c = 9;
console.log(a);//undifined
console.log(b);
console.log(c);
}
f1();
console.log(c);
console.log(b);
console.log(a);//全局
function f1 () {

var a = b = c = 9;//a是局部
console.log(a);
console.log(b);
console.log(c);
}
版权声明
本文为[crystal_iwwish]所创,转载请带上原文链接,感谢
https://blog.csdn.net/crystal_iwwish/article/details/120803469

  1. [skills with annual salary of 60W] after working for 5 years, do you really understand netty and why to use it? (deep dry goods)
  2. Pourquoi MySQL récupère - t - il fréquemment les données ibd?
  3. Tutoriels de téléchargement et d'installation MySQL
  4. In IntelliJ idea, develop a fishing and reading plug-in
  5. Talk about how to integrate SPI with spring
  6. 重学Spring系列之Swagger2.0和Swagger3.0
  7. Hadoop05【命令操作】,Java400道面试题通关宝典助你进大厂
  8. Guava Cache缓存设计原理,java基础入门
  9. Gson:GitHub 标星 18K 的 JSON 解析器,Google 出品的 Java JSON 解析器
  10. gRPC学习之六:gRPC-Gateway集成swagger,java语言程序设计与数据结构进阶版
  11. Projet Java, enregistrer la valeur de modification de la page, utilisé pour les litiges internes
  12. Déterminer si un nombre est un nombre premier - - Java
  13. Sortie 9 * 9 tableau de multiplication - Java
  14. Plusieurs façons de réinitialiser le déplacement du Groupe de consommateurs Kafka
  15. GRPC Learning six: GRPC Gateway Integrated Swagger, Java language programming and data structure Advanced Edition
  16. python数据结构:数组、列表、栈、队列及实现
  17. Gson: json Parser for github Star 18k, Java json Parser from Google
  18. hive学习笔记之二:复杂数据类型,五分钟搞懂MySQL主从复制原理
  19. hive学习笔记之一:基本数据类型,java实现分页技术详解
  20. HDU-3038-How Many Answers Are Wrong【 带权并查集 】题解,java实战项目论坛
  21. HDU 1078 FatMouse and Cheese(记忆化搜索,DP,rocketmq教程教程
  22. Principes de conception du cache Guava, Introduction à la base Java
  23. Un pantalon beige = élégant, blanc + haut de gamme, si beau cet hiver!
  24. Hadoop05 【 commande 】, Java 400 questions d'entrevue pour vous aider à entrer dans l'usine
  25. [Java multithreading] static synchronized method and synchronized (class) code block
  26. [Java multithreading] synchronized statement block
  27. [Java multithreading] synchronized synchronization method
  28. [Java multithreading] thread priority and guard thread
  29. [Java multithreading] stop the thread
  30. Several methods of [Java multithreading] thread
  31. [Java multithreading] Java multithreading skills
  32. Structure des données Python: tableaux, listes, piles, files d'attente et implémentations
  33. Re - Learning Swagger 2.0 and Swagger 3.0 of Spring Series
  34. Error editing HTML5 using webstorm
  35. 【Spring】mybatis-spring
  36. JavaScript post request returned an exception, possibly Cross - Domain request
  37. 图解 Kafka 线程模型及其设计缺陷
  38. Springboot + mybatis Plus + springsecurity + JWT implements user stateless request authentication (front and back end separation)
  39. SQL injection and XSS attack of springboot security vulnerability
  40. Docker employee readme: Why did docker lose to kubernetes?
  41. 19 high force linux commands, hurry to use them!
  42. Add data files for Oracle tablespaces or temporary tablespaces
  43. Intellij IDEA神器居然还有这些小技巧,mysql集群搭建视频
  44. IntelliJ IDEA(2019)之Web项目创建,掌门一对一java面试题
  45. InnoDB(2,如何访问Redis中的海量数据
  46. InheritableThreadLocal使用详解,java多线程面试题及答案整理
  47. How does Oracle modify the data type of a column
  48. Oracle 12C 12.1.0.1.0 management control file official document translation instructions
  49. Oracle 10g 10.2.0.1 in Oracle Linux 5.4 32bit RAC installation manual (Yimo Xiyang)
  50. Oracle 12C in Oracle Linux 6.5 64bit installation manual
  51. 一天十道Java面试题----第一天(面向对象-------》ArrayList和LinkedList)
  52. Schéma du modèle de fil Kafka et de ses défauts de conception
  53. Starting and shutting down Oracle RAC database cluster
  54. CRS_ Oracle CRS stack is already configured and will be running under init(1M)
  55. Common skills of Oracle stored procedure
  56. Check the number of CPUs, core books and threads of the Linux system
  57. Oracle de duplicated data
  58. Oracle creates unique constraints on columns that already have duplicate data
  59. JavaScript-拷贝
  60.  There is ^ [[a garbled code problem in the up and down keys in Oracle sqlplus