当前位置:网站首页>39.js-- scope
39.js-- scope
2022-07-16 05:54:00 【Like to drink pearl milk tea】
Tips : When the article is finished , Directories can be generated automatically , How to generate it, please refer to the help document on the right
1. Definition and rules of scope
var keyword
var fn=; fn It's an identifier
Scope : identifier ( Variable and function names ) Where can I be visited Those places are the scope of this identifier
These places refer to Inside the code block of a function and outside the code block of a function
(1) The rules 1: The code inside the function can access the identifier inside the function , You can also access the identifier outside the function ( side effect )
(2) The rules 2: The identifier outside the function can be accessed outside the function , But you cannot access the identifier inside the function
Generally, the internal identifier of a function is a local identifier
2. Scope classification
Scope is divided into global scope and function scope ( Also called local scope ) Two kinds of
(1) Global scope
All global variables and functions are used as window Object's properties and methods create . Global variables have global scope , stay javascript Everywhere in the code is defined . The global scope will not be destroyed until the application exits, such as closing the web page or browser .
– JS The global variables declared in are the properties of the global object ;
– Function body Don't use var Variables declared and assigned directly are treated as global variables ;
(2) Function scope ( Local scope )
Within the function (var Statement ) The variable of is only defined in the function body . They are local variables , The scope is local . Function arguments are also local variables , They are only defined in the function body . After all the code in the function scope is executed , The scope is destroyed , All the variables and function definitions stored in it are destroyed .
In the body of the function , Local variables take precedence over global variables with the same name , If A local variable declared in a function or a variable with the same name as a global variable in a function parameter , Then the global variable is covered by the local variable .
Case study 1:
function fn () {
a=100
}
var a=200;
fn()
console.log(a)Running results :

Case study 1 notes : Function body does not use var Variables declared and assigned directly are treated as global variables
Case study 2:
function fn (a) {
// var a=300
a=100
}
var a=200;
fn(300)
console.log(a)Running results :

Case study 3:fn(),fm()
function fn (a) {
// Implicitly declare :var a=200
function fm (a) {
console.log(a,122)
a=90
console.log(a,123)
}
fm(a) //fm(200)
console.log(a,124)
}
var a=200;
fn(a) // Implicitly declare :fn(200)
console.log(a,125)Running results :

Case study 4: increment (){ }
var total=0;
function increment () {
var total=0
total=total+2
}
function decrease() {
total=total-2
}
increment()
increment()
decrease()
console.log(total)Running results :

Case study 5: increment (){ }
var total = 0;
function increment() {
// Implicitly declare var total = 0
total = total + 2
console.log(total)
}
increment() // Implicitly declare {var total=0; total = total + 2}
increment() // Implicitly declare {var total=0; total = total + 2}Running results :

Case study 6:fn (){ }
var a = 10
function fn() {
console.log(a)
a = 40
var a = 20
console.log(a)
}
console.log(a)
fn()
console.log(a)Running results :

边栏推荐
猜你喜欢
![[Huang ah code] PHP cooperates with xlwriter to realize infinite header level excel export](/img/42/83660b9b6703881df3370f64a9af78.png)
[Huang ah code] PHP cooperates with xlwriter to realize infinite header level excel export

window系统盘瘦身(开发)

Automated machine learning

开源实习 | Compiler SIG 实习任务正式发布,欢迎报名申请!

Intranet penetration notes - Sticky Keys and system command information collection

Single file component
![[ASIS 2019]Unicorn shop](/img/e2/c295a83e03eeaa720a631f6d736eb2.png)
[ASIS 2019]Unicorn shop

El button display and disable

网络安全应急响应-常用工具

Intranet penetration notes - layer 2 Discovery
随机推荐
Network security emergency response malicious code analysis technology
Automated machine learning
Free CDN jsdelivr acceleration website
2. Memory leak and memory overflow
Buuctf backdoor killing
Php+jmeter simulates high concurrency scenarios. Code attached.
使用crontab遇到的一点问题,报错/var/spool/cron : Permission denied和bash: /usr/bin/chattr: Permission denied
Intranet penetration notes - MSF
PHP对接微信支付--Native tp5框架
ERR_ PNPM_ PEER_ DEP_ ISSUES Unmet peer dependencies
North tour project notes
内网渗透笔记——三层发现and四层发现
Log blacklist can really save you money!
Network security emergency response - basic skills
DVA data flow
Use ramda parsing yarnrc/. Example of npmrc configuration file
Clues in buuctf packets
Notes - Chang Geng
模块化-CMJ&ESM
网络安全应急响应-电子数据取证技术