当前位置:网站首页>匿名函数this指向以及变量提升
匿名函数this指向以及变量提升
2022-06-28 18:30:00 【木头没有瓜】
参考文章: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)
})()
解析:在立即执行函数中,var a = 20; 语句定义了一个局部变量 a,由于js的变量声明提升机制,局部变量a的声明会被提升至立即执行函数的函数体最上方,且由于这样的提升并不包括赋值,因此第一条打印语句会打印undefined,最后一条语句会打印20。
由于变量声明提升,a = 5; 这条语句执行时,局部的变量a已经声明,但是匿名函数的执行是具有全局性的,因此它产生的效果是对局部的变量a赋值,此时window.a 依旧是最开始赋值的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- 函数内部的this指向调用者
- sayOne调用者是person对象,所以this指向person;
- sayTwo的调用者虽然也是person对象,但是区别在于这次调用并没有打出this而是在全局返回了一个匿名函数
- 而这个匿名函数不是作为某个对象的方法来调用执行,是在全局执行
做下修改
var name = 'window'
var person = {
name :'Alan',
sayName:function () {
var that = this
return function () {
console.log(that.name)
}
}
}
person.sayName()() // Alan延伸
【变量提升】下面代码的输出是什么?
function sayHi() {
console.log(name);
console.log(age);
var name = "Lydia";
let age = 21;
}
sayHi();- A:
Lydia和undefined - B:
Lydia和ReferenceError - C:
ReferenceError和21 - D:
undefined和ReferenceError
答案: D
在函数中,我们首先使用var关键字声明了name变量。 这意味着变量在创建阶段会被提升(JavaScript会在创建变量创建阶段为其分配内存空间),默认值为undefined,直到我们实际执行到使用该变量的行。 我们还没有为name变量赋值,所以它仍然保持undefined的值。
使用let关键字(和const)声明的变量也会存在变量提升,但与var不同,初始化没有被提升。 在我们声明(初始化)它们之前,它们是不可访问的。 这被称为“暂时死区”。 当我们在声明变量之前尝试访问变量时,JavaScript会抛出一个ReferenceError。
边栏推荐
- Sword finger offer 11 Minimum number of rotation array
- How to use the current conversion function in SAP CDs view
- Le test de reconnaissance faciale comporte - t - il des préoccupations en matière de protection de la vie privée? Une entreprise étrangère a été arrêtée en cas d'urgence
- 如何高效优雅地管理接口文档
- 几行代码就能实现复杂的 Excel 导入导出,这个工具类真心强大!
- 抗兔Dylight 488丨Abbkine通用型免疫荧光(IF)工具箱
- Advanced technology management - how managers communicate performance and control risks
- Mycat+分库分表
- 324. 摆动排序 II
- February 15, 2022 learning summary
猜你喜欢

How to upgrade from RHEL 8 to RHEL 9

1 invalid import format(s) Postman Collection Format v1 is no longer supported and can not be import

A preliminary study of IO model

数据库对比工具

Sharing-JDBC分布式事务之Seata实现

如何高效优雅地管理接口文档

Industrial digitalization and new generation digitalization system design platform -- Lecture

堆的概念和代码实现

Small program graduation project based on wechat examination small program graduation project opening report function reference

HackTheBox-baby CachedView
随机推荐
Lumiprobe非荧光炔烃研究丨DBCO NHS 酯
东方财富软件股票开户是靠谱的吗?在哪开户安全
手动备份和还原DHCP服务器
JQ plug-in analysis
Konva series tutorial 3: Customizing drawings
golang json 序列化、反序列化 字符串反序列化成 map[string]interface{}
双功能交联剂丨Lumiprobe 磺基花青7二羧酸研究
ANR Application Not Responding
Detailed explanation of select in golang (forward)
推荐两款超高质量的壁纸软件
Small program graduation project based on wechat agricultural and sideline products agricultural products mall small program graduation project opening report function reference
Small program graduation project based on wechat campus lost and found graduation project opening report function reference
324. swing sequencing II
About the solution of "modulenotfounderror: no module named 'flask.\u compat'"
亿信华辰:地产企业数字化转型想要把握时代机遇
Le test de reconnaissance faciale comporte - t - il des préoccupations en matière de protection de la vie privée? Une entreprise étrangère a été arrêtée en cas d'urgence
Advanced technology management - how managers communicate performance and control risks
Small program graduation project based on wechat recruitment small program graduation project opening report function reference
独立站卖家如何高效管理复杂的Facebook主页?
Small program graduation project based on wechat examination small program graduation project opening report function reference