当前位置:网站首页>Detailed explanation of arrow function and this direction
Detailed explanation of arrow function and this direction
2022-07-19 03:32:00 【Nothing * chicken with vegetables】
1. The basic use of arrow functions
- The arrow function is a function of the following form
const func = (paramList) => {
// The body of the function
}
- According to custom , There are not many cases where the arrow function is used as a single function , The arrow function is often used when the parameter to be passed in is a function
// For example, event handler
elem.addEventListener("click", () => {
// The body of the function
})
- There is only one line of code in the function code block
// const mul = (num1, num2) => {
// return num1 + num2
// }
const mul = (num1, num2) => num1 * num2
console.log(mul(20, 30));
2. In the arrow function this The direction of
2. 1 Analysis of a
- We often use function keywords function To declare a function , In a function() in ,this Always points to the object that calls this function .
But the arrow function is this The point is a little special , It always points to... In the nearest outer scope this Referred object
<button id="a"> Ordinary function function </button>
<button id="b"> Use arrow function to construct event handling function </button>
<script>
const a = document.querySelector("#a");
const b = document.querySelector("#b");
// Here are two click events , A bound event handler is a normal function , An arrow function
a.addEventListener("click", function() {
console.log(this);
});
b.addEventListener("click", () => {
console.log(this);
});
</script>
});

You can see from the print result of the console :
Of event handling functions constructed by ordinary functions this It refers to the event source object (button)
In the event handler function constructed by arrow function this It points to... In the scope of its nearest outer function this Point to the Window object 
2. 2 Analysis of the two
const obj = {
a() {
setTimeout(function() {
console.log(this);
})
},
b() {
setTimeout(() => {
console.log(this);
})
}
}
obj.a(); // Print out Window
obj.b(); // Print out The current object obj
This time, function In the constructed function
thisPoints to thewindowobject , And in the arrow function this Points to the nearest In the scope of the outer function this Object to point to obj
problem : In the arrow function this How to find it ?
- answer : To the outer scope , Layer by layer search this, Until there is this The definition of .
const obj = {
aaa() {
setTimeout(function() {
setTimeout(function() {
console.log(this); // window
})
setTimeout(() => {
console.log(this); // window
}
)
})
setTimeout(() => {
setTimeout(function() {
console.log(this); // window
})
setTimeout(() => {
console.log(this); // obj
})
console.log(this); //obj
})
}
}
obj.aaa()
2. 3 this Point to detailed parsing ( Arrow function )
边栏推荐
- Le cinquième jour de trois questions par jour à luogu
- Rhce8 Learning Guide Chapter 1 installing rhel8.4
- 2022-07-16: what is the output of the following go language code? A:[]; B:[5]; C:[5 0 0 0 0]; D:[0 0 0 0 0]。 package main import ( “fmt“ )
- Code demonstration of fcos face detection model in openvino
- Dive Into Deep Learning——2.2数据预处理
- [MCU simulation] (XXI) dB (define byte) - define byte
- 洛谷每日三题之第三天(第四天补做)
- Win10 onedrive failure reinstallation
- Wechat applet -- Summary of problems in the actual development of taro framework
- Chengxin University envi_ IDL first week experiment test: simple operation of array + detailed analysis
猜你喜欢

zsh: command not found: mysql

Yolov5 ncnn reasoning

leetcode162. 寻找峰值

MySQL log management and full backup incremental backup and recovery

Face key point detection

ES6 learning notes - brother Ma at station B

04_ Service registration Eureka

By voting for the destruction of STI by Dao, seektiger is truly community driven

Latest installation tutorial of VMware Tools (rhel8)

論文閱讀:U-Net++: Redesigning Skip Connections to Exploit Multiscale Features in Image Segmentation
随机推荐
二分查找(leetcode704.很简单必会的)
2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)
Ncnn thread
Gdb+vscode for debugging 8 - use core to analyze dead cycles, deadlocks, and segment errors
Go语言中的Iota关键字怎么使用
Visual analysis of ncnn param file and bin model
Net SNMP related commands
Ncnn mat matrix class
Solve the error of 0x00000709 when win10 connects to the shared printer
Examine your investment path
After 4 years of developing two-sided meituan, we finally lost: the interview question of volatile keyword function and principle
2022-07-16: what is the output of the following go language code? A:[]; B:[5]; C:[5 0 0 0 0]; D:[0 0 0 0 0]。 package main import ( “fmt“ )
Affine transformation implementation
Dive Into Deep Learning——2.2数据预处理
By voting for the destruction of STI by Dao, seektiger is truly community driven
Flutter development: running the flutter upgrade command reports an error exception:flutter failed to create a directory at... Solution
ES6学习笔记——B站小马哥
Pytorch best practices and code templates
洛谷每日三题之第三天(第四天补做)
上班摸鱼打卡模拟器微信小程序源码