当前位置:网站首页>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边栏推荐
猜你喜欢

unicloud 云开发记录

一文速学-玩转MySQL获取时间、格式转换各类操作方法详解

The latest MySql installation teaching, very detailed

On the side of Ali, tell me what are the application scenarios of message middleware you know?

Common JVM interview questions and answers

最新MySql安装教学,非常详细

feign调用不通问题,JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r

MySQL compressed package installation, fool teaching
![[Cloud native] Ribbon is no longer used at the bottom layer of OpenFeign starting from the 2020.0.X version](/img/7e/1d27e3f1856ab8c6cbfc5221c717bb.png)
[Cloud native] Ribbon is no longer used at the bottom layer of OpenFeign starting from the 2020.0.X version

Understanding of js arrays
随机推荐
Markdown 帮助文档
自定dialog 布局没有居中解决方案
Android software security and reverse analysis reading notes
2021美赛C题M奖思路
为什么bash中的read要配合while才能读取/dev/stdin的内容
腾讯云GPU桌面服务器驱动安装
【windows】--- SQL Server 2008 超详细安装教程
Several forms of Attribute Changer
[Elastic-Job source code analysis] - job listener
sqlmap injection tutorial common commands
powershell统计文件夹大小
sql 外键约束【表关系绑定】
flutter arr 依赖
jenkins +miniprogram-ci 一键上传微信小程序
【云原生】开源数据分析 SPL 轻松应对 T+0
The MySQL database in Alibaba Cloud was attacked, and the data was finally recovered
function in js
MySQL高级语句(一)
cocos2d-x-3.x 修改和纪录
UiBot存在已打开的MicrosoftEdge浏览器,无法执行安装