当前位置:网站首页>Global scope and function scope in js
Global scope and function scope in js
2022-07-31 05:59:00 【messy me】
Scope:
Scope refers to the scope of the role of a variable. There are two scopes in the street:
Function scope exists within a function, and global variables can be used throughout the program.The relationship between the two scopes is shown in the following figure:

As shown in the figure above, both function scope 1 and function scope 2 are in the global scope.And the two scopes of function scope 1 and function scope 2 are not related to each other, and the variables in each independent function scope do not affect each other.If there is an undefined variable in the function scope, it will be looked for in the global scope.
Global scope:
Global scope:- The js code written directly in the script tag is in the global scope- The global scope is created when the page is opened and destroyed when the page is closed- There is a global object window in the global scope, which represents a browser window,It is created by the browser and we can all use it directly.- In the global scope: the created variables are saved as properties of the window object.The created function will be saved as a method of the window object.- Variables in the global scope are global variables that can be accessed from any part of the page.Early declaration of variables: Variables declared with the var keyword will be declared (but not assigned) before all code is executed.But if the variable is declared without the var keyword, the variable is not declared ahead of time.Such as: console.log(a);var a = 1; is equivalent to a = 1; console.log(a);Advance declaration of functions: functions created using the function declaration form function function(){}It will be created before all the code is executed, so we can call the function before the function declarationFunctions created using function expressions are not declared ahead of time, so they cannot be called before the declaration.Function scope:
function scope- The function scope is created when the function is called, and after the function is executed, the function scope is destroyed.- Every time a function is called, a new function scope is created, and they are independent of each other.- Variables in the global scope can be accessed in the function scope.Function-scoped variables cannot be accessed in the global scope.- When a variable is manipulated in the function scope, it is first searched in its own scope, and if there is one, it is used directly.If not, look for it in the upper scope.If the global scope is still not found, an error will be reported.For example: var a =10 function(){ var a =1 ; console.log(a);};fun() outputs 1.var a =10 function(){ console.log(a);};fun() The output is 10There is also an early declaration feature in the function scope,Variables declared with the var keyword are declared before all the code in the function is executedIn a function, variables declared without var will become global variables边栏推荐
- Chinese garbled solution in UTF-8 environment in Powershell
- 对js的数组的理解
- flutter arr dependencies
- [Cloud native] Open source data analysis SPL easily copes with T+0
- cocos2d-x-3.2 不能混合颜色修改
- 360 hardening file path not exists.
- CMOS管原理,及其在推挽电路中的应用
- Year-end summary - the years are quiet~
- 安装Multisim出现 No software will be installed or removed解决方法
- quick-3.5 无法正常显示有混合纹理的csb文件
猜你喜欢
![[Elastic-Job source code analysis] - job listener](/img/99/5e047b1aa83aad7d7f17b4eec606e6.png)
[Elastic-Job source code analysis] - job listener

MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案

CMOS管原理,及其在推挽电路中的应用

MySql创建数据表

MySql to create data tables

GUCCI, LV and other luxury giant universe how to layout yuan, other brands should keep up with?

Linux modify MySQL database password

2021美赛C题M奖思路

Why is the redis single-threaded also so fast?

For penetration testing methods where the output point is a timestamp (take Oracle database as an example)
随机推荐
[Cloud native] Open source data analysis SPL easily copes with T+0
360 加固 file path not exists.
A simple bash to powershell case
[Ubuntu20.04 installs MySQL and MySQL-workbench visualization tool]
Sqlite A列数据复制到B列
Linux中mysql密码修改方法(亲测可用)
【uiautomation】微信好友列表获取(存储到txt中)
最新MySql安装教学,非常详细
Flutter mixed development module dependencies
sql 外键约束【表关系绑定】
Markdown 帮助文档
powershell统计文件夹大小
带你搞懂MySQL隔离级别,两个事务同时操作同一行数据会怎样?
Gradle sync failed: Uninitialized object exists on backward branch 142
MySql创建数据表
Linux modify MySQL database password
Powershell中UTF-8环境中文乱码解决办法
flutter 混合开发 module 依赖
configure:error no SDL library found
一文速学-玩转MySQL获取时间、格式转换各类操作方法详解