当前位置:网站首页>箭头函数与this指向详解
箭头函数与this指向详解
2022-07-17 01:05:00 【啥也不会的*小菜鸡】
1. 箭头函数的基本使用
- 箭头函数为以下形式的函数
const func = (paramList) => {
// 函数体
}
- 按照习惯,将箭头函数作为单独一个函数来使用的情况并不多,往往是当要传入的参数是函数时才使用箭头函数
// 比如事件处理函数
elem.addEventListener("click", () => {
// 函数体
})
- 函数代码块中只有一行代码
// const mul = (num1, num2) => {
// return num1 + num2
// }
const mul = (num1, num2) => num1 * num2
console.log(mul(20, 30));
2. 箭头函数中this的指向
2. 1 分析一
- 我们常用函数关键字function来声明一个函数,在一个function()中,this总是指向调用这个函数的对象。
但箭头函数的this指向有点特别,它总是指向最近的外层作用域中的this所指对象
<button id="a">普通函数函数</button>
<button id="b">利用箭头函数构造事件处理函数</button>
<script>
const a = document.querySelector("#a");
const b = document.querySelector("#b");
// 这里有两个点击事件,一个绑定的事件处理函数为普通函数,一个为箭头函数
a.addEventListener("click", function() {
console.log(this);
});
b.addEventListener("click", () => {
console.log(this);
});
</script>
});

由控制台打印结果可以看到:
普通函数构造的事件处理函数的this指向的是事件源对象(button)
箭头函数构造的事件处理函数中的this指向的是其最近外层函数作用域中的this指向的Window对象
2. 2 分析二
const obj = {
a() {
setTimeout(function() {
console.log(this);
})
},
b() {
setTimeout(() => {
console.log(this);
})
}
}
obj.a(); // 打印出 Window
obj.b(); // 打印出 当前对象obj
这一次function构造的函数中的
this指向了调用它的window对象,而箭头函数中的this指向了最近的外层函数作用域中的this指向的对象obj
问题: 箭头函数中的this是如何查找的了?
- 答案: 向外层作用域中, 一层层查找this, 直到有this的定义.
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 指向详细解析(箭头函数)
边栏推荐
- Dive Into Deep Learning——2.2数据预处理
- ES6学习笔记——B站小马哥
- MySQL optimized index
- Solve the error of 0x00000709 when win10 connects to the shared printer
- Bisenetv2 face segmentation
- 通过Dao投票STI的销毁,SeekTiger真正做到由社区驱动
- [NoSQL] redis high availability and persistence
- [MySQL] data query operation (select statement)
- [single chip microcomputer simulation] (XI) instruction system logic operation instruction - logic and instruction anl, logic or instruction ORL
- Es6 notes d'étude - station B Xiao Ma Ge
猜你喜欢
![[Jianzhi offer] 31-35 questions (judge whether a sequence is one of the out of stack sequences, sequence print binary tree, branch print, and reverse print each line), judge whether the sequence is th](/img/fd/f0c0e17f2942525a78fbd443514992.png)
[Jianzhi offer] 31-35 questions (judge whether a sequence is one of the out of stack sequences, sequence print binary tree, branch print, and reverse print each line), judge whether the sequence is th

A Youku VIP member account can be used by several people to log in at the same time. How to share multiple people using Youku member accounts?

374. 猜数字大小(入门 必会)

The installation software prompts that the program input point adddlldirectory cannot be located in the dynamic link library kernel32 DLL (download address at the end of the text)

Rtx3090 installing pytorch3d
![mysqldump: [Warning] Using a password on the command line interface can be insecure.](/img/91/8b0d35f85bc0f46daac4e1e9bc9e34.png)
mysqldump: [Warning] Using a password on the command line interface can be insecure.
![[MySQL] MHA high availability](/img/d3/d9830f3c331193fd40b8f00ebe35fa.png)
[MySQL] MHA high availability

Labelme starts normally, but cannot be opened

367. 有效的完全平方数(入门必会)

Yolov5 opencv DNN reasoning
随机推荐
The WinRAR command copies the specified folder as a compressed file, and calls the scheduled task for backup.
Es6 notes d'étude - station B Xiao Ma Ge
Win10 network connection shows no network but Internet access
Bisenetv2 face segmentation
Ncnn thread
Theoretical basis and code implementation of dueling dqn [pytoch + pendulum-v0]
374. 猜数字大小(入门 必会)
SQL classic exercises (x30)
2002 - Can‘t connect to server on ‘127.0.0.1‘ (36)
代理模式——B站动力节点
Visual analysis of ncnn param file and bin model
05_ Service call ribbon
CorelDRAW cannot be installed. Solution
05_服务调用Ribbon
Pytorch best practices and code templates
无线用的鉴权代码
It's good to take more exercise
Need to slow down a little
Flutter development: running the flutter upgrade command reports an error exception:flutter failed to create a directory at... Solution
Transaction and storage engine in MySQL database