当前位置:网站首页>Anonymous function variable problem
Anonymous function variable problem
2022-06-28 18:46:00 【Wood without melon】
var name = 'World!';
(function () {
console.log('name ' + name);
if (typeof name === 'undefined') {
var name = 'Jack';
console.log('Goodbye ' + name);
} else {
console.log('Hello ' + name);
}
})();
name undefined
Goodbye Jack
(1).JavaScript The special thing about variables is , You can reference variables declared later without throwing an exception . This concept is called variable declaration promotion (hoisting);
(2).JavaScript Variables feel like being “ promote ” Or moved to the top of a function or statement . However, the promoted variable will return undefined value .
(3). Declare and initialize after using or referencing a variable , This promoted reference will still get undefined value .
(4). here Example 3 in Test01 Is the key to this problem ,Test01 and Test02 It's very similar ( At least I look similar ). But the final output cannot be (1)、(2)、(3) To explain
After checking the official API Document discovery , In anonymous function (function() {})(); A closure is formed in the use of ( a key , If you are not clear, you can refer to the concept of closures , The concept of closure is easy to understand );
Due to the existence of closures , Global variables are naturally inaccessible ( An important reason for closures is to avoid accessing global variables ), so Test01 function The output of the first sentence in is undefined.
Summarize the reasons : Anonymous functions (function() {})(); Is a special way to write closures .
边栏推荐
- Shell脚本批量修改文件目录权限
- use. NETCORE's own background job, which simply simulates producers and consumers' processing of request response data in and out of the queue
- Chapter 2 processing files, cameras and GUI Cameo applications
- 324. swing sequencing II
- 【Unity3D】相机跟随
- 推荐两款超高质量的壁纸软件
- sqrt()函数的详解和用法「建议收藏」
- 安装nodejs环境
- 技术管理进阶——管理者如何做绩效沟通及把控风险
- Database comparison tool
猜你喜欢

电子商务盛行,怎么提高商店转换率?

POI Excel转换工具

Shanghai Pudong Development Bank Software Test interview real question

刷题分析工具

openGauss内核:SQL解析过程分析

【Unity3D】发射(RayCast)物理射线(Ray)

PHP使用栈解决迷宫问题

进阶高级-业务事务设计 开发入门

Michael Wooldridge, professeur à l'Université d'Oxford: comment la communauté de l'IA voit les réseaux neuronaux depuis près de 40 ans

What are the design requirements for PCB layout and wiring?
随机推荐
618 activity season - the arrival of special discounts for hundreds of low code platforms
浅谈软件研发的复杂性与效能提升之道
Lumiprobe非荧光炔烃研究丨DBCO NHS 酯
Easyexcel learning notes
抗兔Dylight 488丨Abbkine通用型免疫荧光(IF)工具箱
打破学科之间壁垒的STEAM教育
ANR Application Not Responding
数字化转型的1个目标,3大领域,6大因素和9个环节
从理论到实践增强STEAM和工程教育
use. NETCORE's own background job, which simply simulates producers and consumers' processing of request response data in and out of the queue
Concept and code implementation of heap
Enhancing steam and engineering education from theory to practice
C语言文件操作
获取当前日期0点及23点59时间戳
微信小程序接入百度统计报错 Cannot read property ‘mtj‘ of undefined
19.2 容器分类、array、vector容器精解
Seata implementation of sharing JDBC distributed transaction
A preliminary study of IO model
Yixin Huachen: real estate enterprises want to grasp the opportunity of the times for digital transformation
Tensorboard Usage Summary