当前位置:网站首页>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 ”
边栏推荐
- Error: Protobuf syntax version should be first thing in file
- Turn the evolutionary path of push
- [IVI] 17.1 debugging pit FAQ (compilation)
- Getting started with pytoch
- Luogu p4185 [usaco18jan]mootube g problem solution
- mosquitto_ Sub -f parameter use
- What is "enterprise level" low code? Five abilities that must be possessed to become enterprise level low code
- Oncopy and onpaste
- 专访 | 阿里巴巴首席技术官程立:云 + 开源共同形成数字世界的可信基础
- 为什么要使用markdown进行写作?
猜你喜欢

Pytorch 入门

开源峰会抢先看 | 7月29日分论坛&活动议程速览

2018-UperNet ECCV

JVM知识点详细整理(长文警告)

Drunken driving alarm system based on stm32

This is the right way for developers to open artifacts

若依集成minio实现分布式文件存储

How can agile development reduce cognitive bias in collaboration| Agile way

Basic construction of QT project
![[semantic segmentation] 2021-pvt iccv](/img/43/3756c0dbc30fa2871dc8cae5be9bce.png)
[semantic segmentation] 2021-pvt iccv
随机推荐
基于STM32设计的酒驾报警系统
LeetCode_416_分割等和子集
Data visualization design guide (information chart)
LeetCode_278_第一个错误的版本
Basic construction of QT project
8.穿插-从架构设计到实践理解ThreadPoolExecutor线程池
DoD 和 DoR,消减「认知偏差」的两大神器
3道软件测试面试题,能全答对的人不到10%!你会几个?
数据可视化设计指南(信息图表篇)
LeetCode_1049_最后一块石头的重量Ⅱ
[QNX hypervisor 2.2 user manual]7.2.1 hypervisor tracking events
2022cuda summer training camp Day1 practice
Implementation of college logistics repair application system based on SSM
Adcode city code in Gaode map API
重磅 | 基金会为白金、黄金、白银捐赠人授牌
The 2022 open atom global open source summit opened in Beijing
Kunlun storage vs PostgreSQL OLTP test
牛客网刷题
If distributed file storage is realized according to integrated Minio
2022cuda summer training camp Day2 practice