当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
图形系统——2.view绘制
render函数解析
Summary of the use of qobjectcleanuphandler in QT
Detailed explanation of select in golang (forward)
Michael Wooldridge, professeur à l'Université d'Oxford: comment la communauté de l'IA voit les réseaux neuronaux depuis près de 40 ans
PMP怎么补考?补考费用是多少?
leetcode 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers(最少的“二进制数“个数)
devpi
匿名函数变量问题
Huawei cloud AOM released version 2.0, and three features appeared
Professor Michael Wooldridge of Oxford University: how the AI community views neural networks in the past 40 years
Sharing-JDBC分布式事务之Seata实现
curl: (56) Recv failure: Connection reset by peer
An in-depth analysis of the election mechanism in kubernetes
数据资产为王,如何解析企业数字化转型与数据资产管理的关系?
数字化转型的1个目标,3大领域,6大因素和9个环节
基于固态激光雷达辅助的十六线机械雷达和单目相机的外参标定方法
EasyCVR新建用户后,视频调阅页面不能点击的问题修复
CVPR2022 | 浙大、蚂蚁集团提出基于标签关系树的层级残差多粒度分类网络,建模多粒度标签间的层级知识
声网发布灵隼物联网云平台 可一小时构建示例场景









