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

阿里云中mysql数据库被攻击了,最终数据找回来了

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

Chinese garbled solution in UTF-8 environment in Powershell

Principle analysis of famous website msdn.itellyou.cn

UiBot存在已打开的MicrosoftEdge浏览器,无法执行安装

this points to the problem

Artifact SSMwar exploded Error deploying artifact.See server log for details

flutter arr dependencies
![[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

DeFi Token in the project management
随机推荐
quick-3.5 ActionTimeline的setLastFrameCallFunc调用会崩溃问题
MySql创建数据表
(Crypto essential dry goods) Detailed analysis of the current NFT trading markets
ERROR Error: No module factory availabl at Object.PROJECT_CONFIG_JSON_NOT_VALID_OR_NOT_EXIST ‘Error
sqlmap injection tutorial common commands
js中的this指向与原型对象
podspec自动化升级脚本
【云原生】微服务Nacos的简单介绍与使用
Eternal blue bug reappears
著名网站msdn.itellyou.cn原理分析
微信小程序源码获取与反编译方式
Chinese garbled solution in UTF-8 environment in Powershell
Xiaobai learns reptiles - introduction to reptiles
[JVM Loading]---Class Loading Mechanism
function in js
Error: Cannot find module 'D:\Application\nodejs\node_modules\npm\bin\npm-cli.js'
quick-3.6源码修改纪录
The feign call fails, JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r
[windows]--- SQL Server 2008 super detailed installation tutorial
【uiautomation】微信好友列表获取(存储到txt中)