当前位置:网站首页>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 ”
边栏推荐
- ggdag 绘制DAG和因果图
- 使用tidymodels搞定二分类logistic模型
- Survival analysis using rtcga clinical data
- 美团、饿了么被杭州市监约谈要求落实食品安全管理责任 严禁恶意竞争
- Introduction to distributed scheduling xxl-job features
- Two MySQL tables with different codes (utf8, utf8mb4) are joined, resulting in index failure
- Factoextra: visualization of multivariate statistics
- ADB shell WM command and usage:
- Luogu p1816 loyalty solution
- 软件测试干货
猜你喜欢

Summer 2022 software innovation laboratory training JDBC

Second handshake?? Three waves??

LeetCode二叉树系列——144.二叉树的前序遍历

Using Riemann sum to calculate approximate integral in R language

Detailed arrangement of JVM knowledge points (long text warning)

Discussion on the application of arcing smart electricity in elderly care institutions

Tell you from my accident: Mastering asynchrony is key

Turn the evolutionary path of push

Why use markdown to write?

8.穿插-从架构设计到实践理解ThreadPoolExecutor线程池
随机推荐
Using xgboost with tidymodels
Oncopy and onpaste
正则表达式匹配网址
Explore SQL Server metadata (I)
VMware: use commands to update or upgrade VMware esxi hosts
从零开始Blazor Server(3)--添加cookie授权
Mitsubishi PLC and Siemens PLC
R language Monte Carlo method and average method are used to calculate the definite integral. Considering the random point casting method, the confidence is 0.05, and the requirement is ϵ= 0.01, numbe
Scrape crawler framework
A tour of grp:04 - GRP unary call unary call
Tell you from my accident: Mastering asynchrony is key
Data visualization design guide (information chart)
Attachment of text of chenjie Report
Structure the eighth operation of the combat battalion module
12th generation core processor +2.8k OLED ASUS good screen, lingyao 142022 shadow cyan glaze business thin book
What is the difference between a global index and a local index?
2022cuda summer training camp Day6 practice
Add: create Ou structure using PowerShell
Pytorch 入门
GPO:在 Start/Logon 中使用 PowerShell 脚本