当前位置:网站首页>ES6-箭头函数this指向
ES6-箭头函数this指向
2022-07-29 10:43:00 【setTimeout()】
箭头函数内部没有this指向,箭头函数内部this指向只能通过查找作用域链来决定,一旦使用箭头函数当前就不存在作用域链。
下面这样写会报错:
<body>
<script>
let Page = {
id: 123,
init: function() {
document.addEventListener('click', function(e) {
this.todo(e)
})
},
todo: function(type) {
console.log(`事件类型:${type},当前id:${this.id}`);
}
}
Page.init()
</script>
</body>如果改成下面这样:
<body>
<script>
let Page = {
id: 123,
init: function() {
document.addEventListener('click', (e) => {
this.todo(e)
})
},
todo: function(type) {
console.log(`事件类型:${type},当前id:${this.id}`);
}
}
Page.init()
</script>
</body>我们来分析一下上面这个代码,因为在箭头函数里面是没有this指向的,所以在click事件里面的回调函数就没有this,它就会去上一层也就是init去找,init是一个函数,它所在的对象是Page,所以最终作用域是Page,所以最终的输出结果是:

再来看下面这个:
<body>
<script>
let Page = {
id: 123,
init: () => {
document.addEventListener('click', (e) => {
this.todo(e)
})
},
todo: function(type) {
console.log(`事件类型:${type},当前id:${this.id}`);
}
}
Page.init()
</script>
</body>因为箭头函数是没有this指向的,所以在click事件里面的回调函数就没有this,它会去上一层init里面去找,init也是一个箭头函数也没有this指向又到Page,然后找定义Page对象的作用域链就是window。所以结果会报错。
一句话概括就是:
因为箭头函数是没有this指向的,所以在click事件里面的回调函数就没有this,它会去上一层init里面去找,init也是一个箭头函数也没有this指向又到Page,然后找定义Page对象的作用域链就是window。所以结果会报错。
一句话概括就是:“该函数所在作用域指向的对象”
边栏推荐
- Follow teacher Li to learn online generation - matrix (continuously updated)
- Implementation of college logistics repair application system based on SSM
- The server
- Attachment of text of chenjie Report
- Using xgboost with tidymodels
- QT工程基本构建
- Big cloud service company executives changed: technology gives way to sales
- 重磅 | 开放原子校源行活动正式启动
- Kunlunbase instruction manual (II) best practices for peer-to-peer deployment
- 站点数据收集-Scrapy使用笔记
猜你喜欢

为什么要使用markdown进行写作?

98. (cesium chapter) cesium point heat

After E-sports enters Asia, will Tencent be the next "NBA game catcher"?

开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源

Two MySQL tables with different codes (utf8, utf8mb4) are joined, resulting in index failure

Second handshake?? Three waves??

LeetCode二叉树系列——144.二叉树的前序遍历
![[semantic segmentation] 2021-pvt iccv](/img/43/3756c0dbc30fa2871dc8cae5be9bce.png)
[semantic segmentation] 2021-pvt iccv

Implementation of college logistics repair application system based on SSM

MySQL optimization theory study guide
随机推荐
[semantic segmentation] 2021-pvt2 cvmj
Structure the eighth operation of the combat battalion module
Drunken driving alarm system based on stm32
2022cuda summer training camp Day1 practice
为什么要使用markdown进行写作?
What happens when MySQL tables change from compressed tables to ordinary tables
MySQL 8 of relational database -- deepening and comprehensive learning from the inside out
开放原子开源基金会秘书长孙文龙 | 凝心聚力,共拓开源
Follow teacher Wu to learn advanced numbers - function, limit and continuity (continuous update)
阿里架构师耗时一年整理的《Lucene高级文档》,吃透你也是大厂员工!
若依如何实现添加水印功能
Big cloud service company executives changed: technology gives way to sales
DW: optimize the training process of target detection and more comprehensive calculation of positive and negative weights | CVPR 2022
ADB shell WM command and usage:
专访 | 阿里巴巴首席技术官程立:云 + 开源共同形成数字世界的可信基础
Static resource mapping
remap_ Use of table in impdp
【Unity,C#】Character键盘输入转向与旋转
Leetcode binary tree series -- 144. Preorder traversal of binary trees
重磅 | 基金会为白金、黄金、白银捐赠人授牌