当前位置:网站首页>JS scope and pre parsing
JS scope and pre parsing
2022-07-25 11:47:00 【c_ perfectworld】
Scope
Qualified by this name Code scope of availability That's the name Scope . The use of scope increases the limitations of program logic , Enhance program reliability , Reduce name conflicts
It is the code name that plays a role and effect in this range
JS The scope is divided into (es6 Before )
- Global scope
- Local scope
Global scope
Whole script Label or separate one js file
Local scope ( Function scope )
Inside the function , It's a local scope , It only works within functions
Scope of variable
Depending on the scope , Variables can be divided into two types :
- Global variables
- local variable
Global variables
stay Under global scope The variable of
Variables that are not declared inside a function are also global variables
local variable
stay Under local scope The variable of , Or in Internal function The variable of
Be careful :
If it's inside a function , Variables that do not declare direct assignment are also global variables
The formal parameters of a function can also be regarded as local variables
1. In terms of execution efficiency , Global variables are destroyed only when the browser is closed , Compare memory
2. Local variables are destroyed after the program is executed , Saving memory
Block level scope {}
stay {} The variables inside , You can call
Scope chain
- As long as the time code , There is at least one scope
- The local scope written inside the function
- If there are functions in the function , Then a scope can be born in this scope
- According to the mechanism that internal functions can access external function variables , Use chain search to determine which data can be accessed by internal functions , It's called the scope chain
JS Pre parse
js The code is generated by js Parser to execute ,js The parser is running js The code is divided into two steps : Pre parse and Code execution
js The engine will turn js Everything in it var and function Promote to the front of the current scope , Then execute the code in the order of code writing
Pre parsing is divided into variable pre parsing ( Variable Promotion ) And function pre parsing ( Function enhancement )
- Variable promotion is to put all Variable declarations Promote to the front of the current scope , Do not promote assignment operations
- Function promotion is to put all Function declaration Promote to the front of the current scope , Don't call functions
Be careful : The promotion of a function expression must be written below the function expression
边栏推荐
- Dynamic planning problem 03_ Maximum sub segment sum
- 第一个C语言程序(从Hello World开始)
- SQL language (V)
- Shell Chapter 7 exercise
- 菜单栏+状态栏+工具栏==PYQT5
- [MySQL learning 09]
- Details of the list of state products that Apple announced to be eligible for the sales tax holiday in the United States
- SQL注入 Less23(过滤注释符)
- SQL injection LESS18 (header injection + error injection)
- 谣言检测文献阅读十一—Preventing rumor spread with deep learning
猜你喜欢
随机推荐
SQL注入 Less18(头部注入+报错注入)
2022 年中回顾|一文看懂预训练模型最新进展
Linked list related (design linked list and ring linked list)
Review in the middle of 2022 | understand the latest progress of pre training model
Teach you how to configure S2E as the working mode of TCP server through MCU
Shell Chapter 5 homework
Maskgae: masked graph modeling meets graph autoencoders
Various controls ==pyqt5
软件测试阶段的风险
Understanding: idea uses Scala to write wordcount programs and generate jar packages
贪心问题01_活动安排代码分析
JaveScript循环
Esp8266 uses drv8833 drive board to drive N20 motor
W5500 upload temperature and humidity to onenet platform
信号与槽机制==PYQT5
第4章线性方程组
数据库完整性——六大约束学习
OneNET平台控制W5500开发板LED灯
微星主板前面板耳机插孔无声音输出问题【已解决】
Small and micro enterprise smart business card management applet









