当前位置:网站首页>JS作用域以及预解析
JS作用域以及预解析
2022-07-25 10:54:00 【c_perfectworld】
作用域
限定这个名字的可用性的代码范围就是这个名字的作用域。作用域的使用提高了程序逻辑的局限性,增强程序的可靠性,减少名字冲突
就是代码名字在这个范围起作用和效果
JS作用域分为(es6之前)
- 全局作用域
- 局部作用域
全局作用域
整个script标签或单独的一个js文件
局部作用域(函数作用域)
在函数内部,就是局部作用域,只在函数之内起效果
变量的作用域
根据作用域的不同,变量可以分为两种:
- 全局变量
- 局部变量
全局变量
在全局作用域下的变量
在函数内部未声明的变量也是全局变量
局部变量
在局部作用域下的变量,或者在函数内部的变量
注意:
如果在函数内部,没有声明直接赋值的变量也是全局变量
函数的形参也可以看作是局部变量
1.从执行效率来看,全局变量只有在浏览器关闭时才会销毁,比较占内存
2.局部变量在程序执行完毕就会销毁,比较节约内存
块级作用域 {}
在 {} 里面的变量,在外面可以调用
作用域链
- 只要时代码,就至少有一个作用域
- 写在函数内部的局部作用域
- 如果函数中还有函数,那么在这个作用域里面又可以诞生一个作用域
- 根据内部函数可以访问外部函数变量的机制,用链式查找决定哪些数据能被内部函数访问,就称作作用域链
JS预解析
js代码由浏览器中的js解析器来执行,js解析器在运行js代码时分两步:预解析和代码执行
js引擎会把js里面所有的 var 和 function 提升到当前作用域的最前面 , 然后按照代码书写顺序执行代码
预解析分为变量预解析(变量提升)和函数预解析(函数提升)
- 变量提升就是把所有的变量声明提升到当前作用域的最前面,不提升赋值操作
- 函数提升就是把所有的函数声明提升到当前作用域的最前面,不调用函数
注意:函数表达式的提升必须写在函数表达式的下面
边栏推荐
- Getting started with tensorflow
- OneNET平台控制W5500开发板LED灯
- [MySQL learning 08]
- The principle analysis of filter to solve the request parameter garbled code
- Activity registration | play with kubernetes container service improvement class officially opened!
- Shell Chapter 5 homework
- Leetcode sword finger offer 28. symmetric binary tree
- Linked list related (design linked list and ring linked list)
- Plot ==pyqt5
- 各种控件==PYQT5
猜你喜欢
Details of the list of state products that Apple announced to be eligible for the sales tax holiday in the United States

Want to record your supernatural moments when playing games? Let's take a look at how to use unity screenshots

Hacker introductory tutorial (very detailed) from zero basic introduction to proficiency, it is enough to read this one.

Leetcode sword finger offer 28. symmetric binary tree

大话DevOps监控,团队如何选择监控工具?

【mysql学习09】
苹果美国宣布符合销售免税假期的各州产品清单细节

第4章线性方程组

MIIdock简述

Linked list related (design linked list and ring linked list)
随机推荐
模型部署简述
LVS负载均衡之LVS-NAT与LVS-DR模式原理详解
Common web attacks and defense
Onenet platform control w5500 development board LED light
The principle analysis of filter to solve the request parameter garbled code
绘图==PYQT5
论文解读(MaskGAE)《MaskGAE: Masked Graph Modeling Meets Graph Autoencoders》
Shell fourth day homework
Understanding: idea uses Scala to write wordcount programs and generate jar packages
谣言检测文献阅读十一—Preventing rumor spread with deep learning
Small program of vegetable distribution in community
将字符串转换为数字
JVM性能调优方法
Learn NLP with Transformer (Chapter 1)
Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
Menu bar + status bar + toolbar ==pyqt5
教你如何通过MCU配置S2E为TCP Server的工作模式
Shell Chapter 5 homework
Dataframe print ellipsis problem
贪心问题01_活动安排问题