当前位置:网站首页>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边栏推荐
- NFTs: The Heart of Digital Ownership
- podspec 校验依赖出错问题 pod lib lint ,需要指定源
- quick-3.5 无法正常显示有混合纹理的csb文件
- MySQL-如何分库分表?一看就懂
- 【ubuntu20.04安装MySQL以及MySQL-workbench可视化工具】
- 腾讯云GPU桌面服务器驱动安装
- Fragmented NFT (Fractional NFT)
- How to distinguish big and small endian in C language
- 阿里一面,说说你知道消息中间件的应用场景有哪些?
- understand js operators
猜你喜欢

Access database query

【uiautomation】微信好友列表获取(存储到txt中)

Android软件安全与逆向分析阅读笔记

The feign call fails, JSON parse error Illegal character ((CTRL-CHAR, code 31)) only regular white space (r

NFTs: The Heart of Digital Ownership

this points to the problem

2021美赛C题M奖思路

MySql to create data tables

npm WARN config global `--global`, `--local` are deprecated. Use `--location solution
![[Cloud native] Simple introduction and use of microservice Nacos](/img/06/b0594208d5b0cbf3ae8edd80ec12c4.png)
[Cloud native] Simple introduction and use of microservice Nacos
随机推荐
The MySQL database in Alibaba Cloud was attacked, and the data was finally recovered
[swagger close] The production environment closes the swagger method
GUCCI, LV and other luxury giant universe how to layout yuan, other brands should keep up with?
Build vulhub vulnerability shooting range on kali
sql add default constraint
Flow control statement in js
Pure shell implementation of text replacement
js中的函数
自定dialog 布局没有居中解决方案
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
动态规划(一)| 斐波那契数列和归递
flutter arr dependencies
flutter arr 依赖
MySQL分页查询的5种方法
What is GameFi?
cocos2d-x 实现跨平台的目录遍历
sql 外键约束【表关系绑定】
Attribute Changer的几种形态
quick-3.5 无法正常显示有混合纹理的csb文件
Artifact SSMwar exploded Error deploying artifact.See server log for details