当前位置:网站首页>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 ”
边栏推荐
- NUMA architecture CPU API change summary
- 12th generation core processor +2.8k OLED ASUS good screen, lingyao 142022 shadow cyan glaze business thin book
- 重磅 | 开放原子校源行活动正式启动
- VMWare:使用命令更新或升级 VMWare ESXi 主机
- 面试中项目讲解的步骤
- Svn revision keyword
- Using R-Pack premsim to predict microsatellite instability based on gene expression
- Oncopy and onpaste
- Learning R language these ebooks are enough!
- [semantic segmentation] 2021-pvt iccv
猜你喜欢

Second handshake?? Three waves??

Kunlunbase instruction manual (I) quick installation manual

2018-UperNet ECCV

QT工程基本构建

Using Riemann sum to calculate approximate integral in R language

Leetcode binary tree series -- 144. Preorder traversal of binary trees

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

Kunlun storage vs PostgreSQL OLTP test

阿里架构师耗时一年整理的《Lucene高级文档》,吃透你也是大厂员工!

Object storage
随机推荐
Static resource mapping
LeetCode_416_分割等和子集
R 语言 BRCA.mRNA数据集 分析
Kunlunbase instruction manual (IV) real time synchronization of data from Oracle to kunlunbase
LeetCode_1049_最后一块石头的重量Ⅱ
Drawing box and ellipse of WPF screenshot control (IV) "imitating wechat"
基于STM32设计的酒驾报警系统
Zhou Hongyi: 360 is the largest secure big data company in the world
重磅 | 2022 开放原子全球开源峰会在北京开幕
Kunlunbase instruction manual (III) data import & synchronization
AI模型风险评估 第2部分:核心内容
软件测试干货
R 语言 Monte Carlo方法 和平均值法 计算定积分, 考虑随机投点法,计算在置信度0.05, 要求为ϵ=0.01 , 所需要的试验次数
Leetcode binary tree series -- 144. Preorder traversal of binary trees
Survival analysis using rtcga clinical data
Evolution of xxl-job architecture for distributed scheduling
服务器
Tell you from my accident: Mastering asynchrony is key
Drunken driving alarm system based on stm32
Error: Protobuf syntax version should be first thing in file