当前位置:网站首页>Anonymous function this pointing and variable promotion
Anonymous function this pointing and variable promotion
2022-06-28 18:47:00 【Wood without melon】
Reference article :https://www.jb51.net/article/161019.htm
var a = 10;
(function() {
console.log(a)
a = 5
console.log(window.a)
var a = 20;
console.log(a)
})()
analysis : In the immediate function ,var a = 20; Statement defines a local variable a, because js Variable declaration promotion mechanism , local variable a The declaration of is promoted to the top of the body of the function that executes the function immediately , And since such promotion does not include assignment , So the first print statement prints undefined, The last statement will print 20.
Due to variable declaration Promotion ,a = 5; When this statement is executed , Local variables a Already declared , however The execution of anonymous functions is global , Therefore, its effect is on local variables a assignment , here window.a It is still the first assignment 10.
var name = 'window'
var person = {
name: 'Alan',
sayOne: function() {
console.log(this.name)
},
sayTwo: function() {
return function() {
console.log(this.name)
}
}
}
person.sayOne() //Alan
person.sayTwo()() // window- intra-function this Point to the caller
- sayOne The caller is person object , therefore this Point to person;
- sayTwo Although the caller of is also person object , But the difference is that this call doesn't type this Instead, an anonymous function is returned globally
- This anonymous function is not called and executed as a method of an object , Is performed globally
Make some changes
var name = 'window'
var person = {
name :'Alan',
sayName:function () {
var that = this
return function () {
console.log(that.name)
}
}
}
person.sayName()() // Alanextend
【 Variable Promotion 】 What is the output of the following code ?
function sayHi() {
console.log(name);
console.log(age);
var name = "Lydia";
let age = 21;
}
sayHi();- A:
Lydiaandundefined - B:
LydiaandReferenceError - C:
ReferenceErrorand21 - D:
undefinedandReferenceError
answer : D
In the function , We use var The keyword declares name Variable . This means that the variable will be promoted during the creation phase (JavaScript Memory space will be allocated to the variable during the creation phase ), The default value is undefined, Until we actually execute to the line using the variable . We haven't name Variable assignment , So it still remains undefined Value .
Use let keyword ( and const) Declared variables also have variable promotion , But with var Different , Initialization is not promoted . In our statement ( initialization ) Before they , They are inaccessible . This is known as “ A temporary dead zone ”. When we try to access a variable before declaring it ,JavaScript Will throw out a ReferenceError.
边栏推荐
猜你喜欢

C# 41. int与string互转

牛津大学教授Michael Wooldridge:AI社区近40年如何看待神经网络

POI Excel转换工具

PCB线路板布局和布线都有哪些设计要求?

Go, begin, end, for, after, instead of

Professor Michael Wooldridge of Oxford University: how the AI community views neural networks in the past 40 years

被315点名的流氓下载器,又回来了…

CVPR2022 | 浙大、蚂蚁集团提出基于标签关系树的层级残差多粒度分类网络,建模多粒度标签间的层级知识

新工作第一天

Graphic system - 1 Layout loading
随机推荐
Go, begin, end, for, after, instead of
好用、强大的PDF 阅读软件综合评测:PDF Expert 、MarginNote、LiquidText、Notability、GoodNotes、Zotero
Operations research note
启牛学堂的vip证券账户是真的安全正规吗?怎么说
基于管线的混合渲染
微信小程序接入百度统计报错 Cannot read property ‘mtj‘ of undefined
devpi
进阶高级-业务事务设计 开发入门
curl: (56) Recv failure: Connection reset by peer
Enhancing steam and engineering education from theory to practice
Openfire 3.8.2集群配置
tensorboard 使用总结
About Covariance and Correlation(协方差和相关)
正版ST-link/V2 J-LINK JTAG/SWD引脚定义和注意事项
【软件测试】2022年普通高等学校招生全国统一考试
上传文件列表(文件名重复加括号标识)
Upload file list (repeated file names are marked with brackets)
华为云AOM发布2.0版本,3大特性亮相
PCB线路板布局和布线都有哪些设计要求?
Alist+RaiDrive 给电脑整个80亿GB硬盘