当前位置:网站首页>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
- MySQL高级语句(一)
- Yuan prospect and four track of the universe
- js中流程控制语句
- Notes on creating a new virtual machine in Hyper-V
- UiBot存在已打开的MicrosoftEdge浏览器,无法执行安装
- How to distinguish big and small endian in C language
- function in js
- Filter out egrep itself when using ps | egrep
- 微信小程序启动优化
猜你喜欢
数据库 | SQL增删改查基础语法
js中的全局作用域与函数作用域
jenkins +miniprogram-ci 一键上传微信小程序
Build DVWA with phpstudy
计网 Packet Tracer仿真 | 简单易懂集线器和交换机对比(理论+仿真)
Error: Cannot find module 'D:\Application\nodejs\node_modules\npm\bin\npm-cli.js'
this指向问题
MySql to create data tables
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
常见JVM面试题及答案整理
随机推荐
场效应管 | N-mos内部结构详解
UiBot存在已打开的MicrosoftEdge浏览器,无法执行安装
Using IIS10 to build an asp website in win11
Markdown 帮助文档
sql add default constraint
powershell统计文件夹大小
configure:error no SDL library found
podspec 校验依赖出错问题 pod lib lint ,需要指定源
sql 外键约束【表关系绑定】
5 methods of MySQL paging query
cocos2d-x-3.x 修改和纪录
Several solutions for mysql startup error The server quit without updating PID file
【uiautomation】微信好友列表获取(存储到txt中)
Build DVWA with phpstudy
MySql创建数据表
数据库 | SQL增删改查基础语法
Attribute Changer的几种形态
this points to the problem
sqlite 查看表结构 android.database.sqlite.SQLiteException: table splitTable has no column named
npm WARN config global `--global`, `--local` are deprecated. Use `--location解决方案