当前位置:网站首页>ES6 arrow function this points to
ES6 arrow function this points to
2022-07-29 10:50:00 【setTimeout()】
There is no... Inside the arrow function this Point to , Inside the arrow function this The direction can only be determined by looking up the scope chain , Once the arrow function is used, there is currently no scope chain .
Writing like this will report an error :
<body>
<script>
let Page = {
id: 123,
init: function() {
document.addEventListener('click', function(e) {
this.todo(e)
})
},
todo: function(type) {
console.log(` Event type :${type}, At present id:${this.id}`);
}
}
Page.init()
</script>
</body>If you change it to the following :
<body>
<script>
let Page = {
id: 123,
init: function() {
document.addEventListener('click', (e) => {
this.todo(e)
})
},
todo: function(type) {
console.log(` Event type :${type}, At present id:${this.id}`);
}
}
Page.init()
</script>
</body>Let's analyze the above code , Because in the arrow function, there is no this Point to the , So in click There is no callback function in the event this, It will go to the next floor, that is init Look for ,init It's a function , Its object is Page, So the final scope is Page, So the final output is :

Let's take a look at the following :
<body>
<script>
let Page = {
id: 123,
init: () => {
document.addEventListener('click', (e) => {
this.todo(e)
})
},
todo: function(type) {
console.log(` Event type :${type}, At present id:${this.id}`);
}
}
Page.init()
</script>
</body>Because the arrow function doesn't have this Point to the , So in click There is no callback function in the event this, It will go to the next floor init Go inside ,init It is also an arrow function without this Point to again Page, Then find the definition Page The scope chain of an object is window. So the result will be wrong .
In a word, it is :
Because the arrow function doesn't have this Point to the , So in click There is no callback function in the event this, It will go to the next floor init Go inside ,init It is also an arrow function without this Point to again Page, Then find the definition Page The scope chain of an object is window. So the result will be wrong .
In a word, it is :“ The object pointed to by the scope of this function ”
边栏推荐
- Conference OA project - my approval
- Drunken driving alarm system based on stm32
- WPF 截图控件之绘制方框与椭圆(四) 「仿微信」
- ADDS:使用 PowerShell 创建 OU 结构
- Using R-Pack premsim to predict microsatellite instability based on gene expression
- R language brca MRNA data set analysis
- Basic construction of QT project
- 为什么要使用markdown进行写作?
- Structure the eighth operation of the combat battalion module
- 重磅 | 2022 开放原子全球开源峰会在北京开幕
猜你喜欢

Drunken driving alarm system based on stm32

Static resource mapping

Kunlunbase instruction manual (III) data import & synchronization

周鸿祎:360是世界上最大的安全大数据公司

How beautiful can VIM be configured?

Kunlunbase instruction manual (I) quick installation manual

浅谈安科瑞灭弧式智慧用电在养老机构的应用

Pytorch 入门

专访 | 阿里巴巴首席技术官程立:云 + 开源共同形成数字世界的可信基础

R 语言 用黎曼和求近似 积分
随机推荐
重磅 | 基金会为白金、黄金、白银捐赠人授牌
What happens when MySQL tables change from compressed tables to ordinary tables
PAHO cross compilation
从零开始Blazor Server(3)--添加cookie授权
Survival analysis using rtcga clinical data
R language brca MRNA data set analysis
If distributed file storage is realized according to integrated Minio
2022cuda summer training camp day3 practice
Introduction to distributed scheduling xxl-job features
JVM知识点详细整理(长文警告)
若依集成minio实现分布式文件存储
深度强化学习应用实践技巧
How to realize the function of adding watermark
Kunlun storage vs PostgreSQL OLTP test
为什么要使用markdown进行写作?
使用R包PreMSIm根据基因表达量来预测微卫星不稳定
2018-UperNet ECCV
Data visualization design guide (information chart)
R 语言 BRCA.mRNA数据集 分析
Drawing box and ellipse of WPF screenshot control (IV) "imitating wechat"