当前位置:网站首页>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。所以结果会报错。
一句话概括就是:“该函数所在作用域指向的对象”
边栏推荐
- If distributed file storage is realized according to integrated Minio
- Understand what a binary tree is (types, traversal methods, definitions of binary trees)
- 若依如何实现添加水印功能
- 3道软件测试面试题,能全答对的人不到10%!你会几个?
- 2022cuda summer training camp Day5 practice
- Kunlun storage vs PostgreSQL OLTP test
- Data visualization design guide (information chart)
- Scrape crawler framework
- R 语言 用黎曼和求近似 积分
- 为什么要使用markdown进行写作?
猜你喜欢

Second handshake?? Three waves??

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

Meeting OA project (V) -- meeting notice and feedback details

If distributed file storage is realized according to integrated Minio

Follow teacher Tian to learn practical English Grammar (continuous update)

NUMA architecture CPU API change summary

Pytorch 入门

How to realize the function of adding watermark

WPF 截图控件之绘制方框与椭圆(四) 「仿微信」

QT工程基本构建
随机推荐
重磅 | 开放原子校源行活动正式启动
Software testing dry goods
架构实战营模块八作业
ECCV 2022 | CMU proposes to recurse on the visual transformer without adding parameters, and the amount of calculation is still small
Kunlunbase instruction manual (I) quick installation manual
Error: Protobuf syntax version should be first thing in file
R 语言 Monte Carlo方法 和平均值法 计算定积分, 考虑随机投点法,计算在置信度0.05, 要求为ϵ=0.01 , 所需要的试验次数
Implementation of college logistics repair application system based on SSM
VMware: use commands to update or upgrade VMware esxi hosts
factoextra:多元统计方法的可视化PCA
R 语言 二分法与 牛顿迭代法计算中方程的根
Attachment of text of chenjie Report
Analysis of QT basic engineering
Survival analysis using rtcga clinical data
站点数据收集-Scrapy使用笔记
MySQL optimization theory study guide
Review of the 16th issue of HMS core discovery | play with the new "sound" state of AI with tiger pier
What is the difference between a global index and a local index?
Using xgboost with tidymodels
Adcode city code in Gaode map API