当前位置:网站首页>JS [detailed explanation] scope
JS [detailed explanation] scope
2022-04-22 11:59:00 【Chaoyang 39】
Scope is variable / The area in which the function can be accessed .
Global scope
{} And the area outside the function is the global scope .
- The declared variables in the global scope are global variables , It can be accessed in any part of the page .
- Variables of function scope cannot be accessed in global scope
- The global scope is created when the page opens , Destroy when the page is closed .
- There is a global object in the global scope window, It represents a browser window , Created by browser , You can use it directly , The global variable is window Object properties , The function is window Object method .
function foo() {
var a = b = 100; // Continuous assignment
}
foo();
console.log(window.b); // Print 100
console.log(window.a); // Print undefined
console.log(a); // Report errors Uncaught ReferenceError: a is not defined
var a = b = 100 This line of consecutive assignment code is equivalent to var a = (b = 100), The order of execution is :
- The first 100 Assign a value to b;
- And then declare variables a
- And then b The value of is assigned to a
- b An undeclared variable is assigned a value , Belong to window.b;
- a The scope of is limited to foo() Internal function , Do not belong to window;
Block scope
{} Block scope in
- Variables are only valid in code blocks ;
ES5 Itself does not provide block level scope , We usually use an immediate call function expression to implement
(function () {
var block_scoped=0;
}());
console.log(block_scoped); // Misquote
ES6 in , Use let and const Implement block level scope
let a =1
if(a===1) {
let b = 2
}
console.log(b); // Uncaught ReferenceError: b is not defined
Function scope
The area within the function is the function scope ( namely function in { } The inner part of )
The principle of function scope
Execution context : When the function executes , An internal object of the execution time context will be created . Every time a function is called , A new context object will be created , They are independent of each other . When the function is finished executing , The execution context it generates is destroyed .
When operating on a variable in the scope of a function , It will first look for , If there is, use it directly ( Nearby principle ). If not, look up to the next level of scope for , Until the global scope is found ; If the global scope is still missing , May be an error ReferenceError.
Characteristics of function scope
- Variables in the function scope that can access the global scope , Such as window.a( Both the global scope and the function scope define variables a when )
- The global scope cannot access variables defined within a function
- Function , Use var Keyword declared variables , Will be declared before all code in the function executes
- Function , No, var Declared variables are global variables , And it doesn't say in advance .
- Function , Function declarations are also executed before all code in the function is executed
var a = 1;
function foo() {
console.log(a);
a = 2; // Here a amount to window.a
}
foo(); // The printout is 1
console.log(a); // The printout is 2
Defining a parameter is equivalent to declaring a variable in the scope of a function .
function fun6(e) {
// In this function , Because of the formal parameters e, This is equivalent to the first line of code inside the function , Yes var e;
console.log(e);
}
fun6(); // The result is undefined
fun6(123);// The result is 123
版权声明
本文为[Chaoyang 39]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204221159030297.html
边栏推荐
- Summary of important knowledge points of discrete structure and its application
- Mysql基本操作
- C language small project ---- > push box
- 【深入理解TcaplusDB技术】读取列表指定位置数据接口说明——[List表]
- Golang development: suggestions for go concurrency
- 1086 Tree Traversals Again (25 分)
- uniApp 学习笔记总结(一)
- 函数的栈帧理解
- Game + NFT, another feasible scene from virtual to real
- Go developer survey: 92% of developers are satisfied with go
猜你喜欢
随机推荐
在VSCode中设置滑动滚轮改变字体大小
离散结构及其应用重要知识点总结
在Docker环境上使用Debezium捕获PostgreSQL 14.2中的变更数据到Kafka
Why can't people see the truth?
离职的互联网人,都去哪儿了?
Flutter 应用程序创建一个扩展面板列表
Fee details page
uniApp 学习笔记总结(一)
In depth report: in the heterogeneous era, chips need to integrate multiple templates
封装的方法2
活动预告 | 4月23日,多场OpenMLDB精彩分享来袭,不负周末好时光!
LeetCode383. Ransom letter
云中漫步-旅行到宇宙边缘
Stack frame understanding of function
Ptorch quantification (torch. Quantification)
订单详情页面
Leetcode Tencent selected exercises 50 questions - 104 Maximum depth of binary tree
实操教程|Pytorch - 弹性训练极简实现( 附源码)
将博客搬至CSDN
云融科技加入龙蜥社区,助力金融行业数字化转型









