当前位置:网站首页>匿名函数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。
边栏推荐
- How to use the current conversion function in SAP CDs view
- How to design a business high performance and high availability computing architecture - job
- 【云驻共创】昇腾异构计算架构CANN,助力释放硬件澎湃算力
- Go 降序排序 取 Top N
- sqrt()函数的详解和用法「建议收藏」
- 基于固态激光雷达辅助的十六线机械雷达和单目相机的外参标定方法
- 亿信华辰:地产企业数字化转型想要把握时代机遇
- Small program graduation project based on wechat agricultural and sideline products agricultural products mall small program graduation project opening report function reference
- NFT流动性协议的安全困局—NFT借贷协议XCarnival被黑事件分析
- 启牛学堂的vip证券账户是真的安全正规吗?怎么说
猜你喜欢

Applet graduation design based on wechat gym private education appointment applet graduation design opening report function reference

IDM certification process log embedding point description

Lumiprobe丨Lumizol RNA 提取试剂解决方案

面部识别试验涉及隐私安全问题?国外一公司被紧急叫停

双功能交联剂丨Lumiprobe 磺基花青7二羧酸研究

Seata implementation of sharing JDBC distributed transaction

Record an emotet Trojan horse handling case

微软独家付费功能,也被完美解锁了

China gaobang brand story: standing guard for safety, gaobang pays attention to

19.2 容器分类、array、vector容器精解
随机推荐
微软独家付费功能,也被完美解锁了
Mycat+ sub database and sub table
Does face recognition test involve privacy and security issues? A foreign company was urgently stopped
面部識別試驗涉及隱私安全問題?國外一公司被緊急叫停
Konva series tutorial 3: Customizing drawings
select/poll/epoll
手动备份和还原DHCP服务器
Three communication skills in software testing
[translation] list of Clickhouse 22.4 and 22.5 core features
GCC getting started manual
注意!PMP紧急缓考今天就截止了!
电子商务盛行,怎么提高商店转换率?
leetcode 1423. Maximum Points You Can Obtain from Cards(从牌中能得到的最大点数和)
render函数解析
Detailed explanation of select in golang (forward)
Lumiprobe非荧光炔烃研究丨DBCO NHS 酯
Applet graduation design based on wechat gym private education appointment applet graduation design opening report function reference
1 invalid import format(s) Postman Collection Format v1 is no longer supported and can not be import
中金财富开户安全吗?开过中金财富的讲一下
leetcode 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers(最少的“二进制数“个数)