当前位置:网站首页>JS deep understanding of scope
JS deep understanding of scope
2022-06-24 18:36:00 【Brother Mengfan】
Catalog
3、 ... and 、 Scope internal principle
5、 ... and 、 The shadowing effect
One 、 What is scope
stay JavaScript There are two scope types in :
(1) Global scope
(2) Function scope ( Local scope )
Two 、 Scope
2.1、 Global scope
<script>
console.log(window);
var a = 10;
console.log(window.a);
function aa() {
console.log('aa')
}
window.aa();
var aaa = {
name: ' Zhang San '
}
console.log(window.aaa.name);
</script>2.2、 Function scope
<script>
var age = 18;
function fn() {
var age = 20;
console.log(age); // 20
console.log(this.age); // 18
//console.log(this);
console.log(window.age); // 18
}
window.fn();
</script>2.3、 Automatic global scope
function fn() {
age = 19;
console.log(age);
}
fn();
console.log(window);
console.log(age);3、 ... and 、 Scope internal principle
3.1、 Compile phase
The process of compiling is that the compiler decomposes the program into lexical units , Parse lexical units into AST, And then AST To convert to a machine means Make , The process of waiting for execution .
3.2、 Execution phase
var a = 2;
console.log(a);3.3、 Query phase
var a = 2;// LSH Inquire about
function add() {
return 2;
}
var b = add(); // RHS Inquire about function fn(a) {
console.log(a);
}
fn(1);3.4、 Nesting stage
The following is an example to illustrate the search mechanism for scope variables .
function fn(a) {
console.log(a + b);
}
var b = 2;
fn(4);
3.5、 Abnormal stage
function fn(a) {
a = b; // b is not defined
}
fn(2);
function fn2() {
var b = 0;
b(); // b is not a function
}
fn2();
function fn3() {
a = 1;
}
fn(); // If you do not execute this , Will be submitted to the a is not definedcase analysis :
function fn(a) {
console.log(a);
}
fn(2);Four 、 Lexical scope
Lexical scope refers to all declared identifiers under the current scope .
function fn(a) {
var b = a * 2;
function bar(c) {
console.log(a, b, c);//2 4 12
}
bar(b * 3);
}
fn(2);
5、 ... and 、 The shadowing effect
<script>
var a = 0;
function fn() {
var a = 1;
console.log(a);
}
fn();
</script>边栏推荐
- TCE was shortlisted as a typical solution for ICT innovation of the Ministry of industry and information technology in 2020
- Eight digit
- Differences between get and post request modes
- Complete Guide to web application penetration testing
- 717.1-bit and 2-bit characters [sliding window]
- Several key points for enterprises to pay attention to digital transformation
- About pyqt5 to realize paging function (one window implements different interfaces)
- Business based precipitation component = & gt; manage-table
- How does the video platform import the old database into the new database?
- Crmeb multi merchant PC packaging tutorial
猜你喜欢

Leetcode weekly buckle 281
Online sequence flow chart making tool

Number of occurrences of numbers in the array (medium difficulty)

Two micro service interviews where small companies suffer losses

Specification for self test requirements of program developers

SAP license: ERP for supply chain management and Implementation

How to select the best test cases for automation?

Network security database penetration of secondary vocational group in 2022

13 skills necessary for a competent QA Manager
![[untitled]](/img/ab/066923f1aa1e8dd8dcc572cb60a25d.jpg)
[untitled]
随机推荐
C language - structure II
Optimizing bloom filter: challenges, solutions, and comparisons
Selection (030) - what is the output of the following code?
SAP license:sap s/4hana is the answer
What makes data analysts good- Cassie Kozyrkov
SDL: cannot play audio after upgrading openaudio to openaudiodevice
Application service access configuration parameters
Uniapp wechat applet calls mobile map to navigate to the target point
About whether arm's large and small end mode is related to CPU or compiler
The country has made a move! Launch network security review on HowNet
Easynvr fails to use onvif to detect the device. What is the reason why "no data" is displayed?
It is often blocked by R & D and operation? You need to master the 8 steps before realizing the requirements
Vite+web3:报错出现ReferenceError: process is not defined
An analysis of the comments on the TV series Douban by procedural apes
如何在 R 中创建线性模型预测区间 并可视化
ASP. Net hosting uploading file message 500 error in IIS
Business based precipitation component = & gt; manage-table
High quality defect analysis: let yourself write fewer bugs
微服务系统设计——数据模型与系统架构设计
Data driven decision making: Decision intelligence and design thinking