当前位置:网站首页>箭头函数与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 指向详细解析(箭头函数)
边栏推荐
- [MCU simulation] (V) addressing mode - immediate addressing and register indirect addressing
- 05_ Service call ribbon
- While loop
- Envi: (the most detailed tutorial in 2022) custom coordinate system
- module_ Basic principle of init function
- Ubuntu clear CUDA cache
- Basic IDL content of note 1: common data types_ Create array_ Type conversion_ Print output_ Basic operation_ Relational operation
- 重写equals为什么要重写hashcode
- Configure high availability using virtual ip+kept
- [MySQL] MHA high availability
猜你喜欢
![Monte Carlo based reinforcement learning method [with code implementation]](/img/39/346b2f4122238eb0d51ca164ab6d86.png)
Monte Carlo based reinforcement learning method [with code implementation]

Browser cannot open tensorboard

Display zabbix6.0 information using grafana8.5.2
![[MCU simulation] (XX) org - set start address](/img/9e/4e44dd779b0de28a190d86fbb1c2c0.png)
[MCU simulation] (XX) org - set start address

【模板记录】字符串哈希判断回文串

Affine transformation implementation

leetcode162. 寻找峰值

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

Yolov6 learning first chapter

Automatic assembly & set injection
随机推荐
Visual analysis of ncnn param file and bin model
Ncnn mat matrix class
leetcode:50. Pow(x, n)
Transaction and storage engine in MySQL database
论文阅读:U-Net++: Redesigning Skip Connections to Exploit Multiscale Features in Image Segmentation
通过OpenHarmony兼容性测评,大师兄开发板与丰富教培资源已ready
oracle 查询非自增长分区的最大分区
Rhce8 Learning Guide Chapter 1 installing rhel8.4
Wechat applet
oracle 关闭回收站
Zabbix6.0 monitors Dell and IBM server hardware through Idrac and imm2
First knowledge of JPA (ORM idea, basic operation of JPA)
Leetcode: dynamic programming [basic problem solving]
Bisenetv2 face segmentation
Snapshot: data snapshot (data disclosure method)
Net SNMP development I
Redis和其他数据库的比较
Flutter开发:运行flutter upgrade命令报错Exception:Flutter failed to create a directory at…解决方法
Yolov6 learning first chapter
Chengxin University envi_ The second week of IDL experiment content: extract aod+ in all MODIS aerosol products for detailed analysis