当前位置:网站首页>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
边栏推荐
- 动态规划问题03_最大子段和
- Reptile foundation I
- Teach you how to configure S2E as the working mode of TCP server through MCU
- 将字符串转换为数字
- Various controls ==pyqt5
- ESP8266 使用 DRV8833驱动板驱动N20电机
- 模型部署简述
- Shell - Chapter 8 exercise
- Information management system for typical works of urban sculpture (picture sharing system SSM)
- JS常用内置对象 数据类型的分类 传参 堆栈
猜你喜欢

WIZnet嵌入式以太网技术培训公开课(免费!!!)

软件测试阶段的风险

How to judge the performance of static code quality analysis tools? These five factors must be considered

微星主板前面板耳机插孔无声音输出问题【已解决】

基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现

Summary of combination problems of Li Kou brush questions (backtracking)

教你如何通过MCU将S2E配置为UDP的工作模式

城市雕塑典型作品信息管理系统(图片分享系统SSM)

常见WEB攻击与防御

软件缺陷的管理
随机推荐
任何时间,任何地点,超级侦探,认真办案!
【USB设备设计】--复合设备,双HID高速(64Byte 和 1024Byte)
Shell - Chapter 8 exercise
菜单栏+状态栏+工具栏==PYQT5
How to judge the performance of static code quality analysis tools? These five factors must be considered
Review in the middle of 2022 | understand the latest progress of pre training model
SQL注入 Less23(过滤注释符)
varest蓝图设置json
Reptile foundation I
硬件外设=maixpy3
【mysql学习08】
动态规划问题05_导弹拦截
Leetcode sword finger offer 27. image of binary tree
全网显示 IP 归属地,是怎么实现的?
W5500通过上位机控制实现调节LED灯带的亮度
Common linear modulation methods based on MATLAB
信号与槽机制==PYQT5
教你如何通过MCU配置S2E为TCP Server的工作模式
用 Redis 做一个可靠的延迟队列
Eigenvalues and eigenvectors of matrices