当前位置:网站首页>箭头函数与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 指向详细解析(箭头函数)
边栏推荐
- 二分查找(leetcode704.很简单必会的)
- Es6 notes d'étude - station B Xiao Ma Ge
- [MCU simulation] (XVI) control transfer instructions - unconditional transfer instructions, conditional transfer instructions
- oracle 关闭回收站
- [MCU simulation] (XIX) introduction to assembly, assembly instructions, pseudo instructions
- MySQL面试题(2022)
- D. Permutation Restoration(贪心/双指针/set)
- MySQL optimized index
- Chengxin University envi_ IDL second week homework: extract aerosol thickness at n points + detailed analysis
- [MCU simulation] (XX) org - set start address
猜你喜欢

05_服务调用Ribbon
![Monte Carlo based reinforcement learning method [with code implementation]](/img/39/346b2f4122238eb0d51ca164ab6d86.png)
Monte Carlo based reinforcement learning method [with code implementation]

Pytorch best practices and code templates

Bisenetv2 face segmentation

Comparison between redis and other databases

CorelDRAW cannot be installed. Solution

通过OpenHarmony兼容性测评,大师兄开发板与丰富教培资源已ready

Rhce8 Study Guide Chapter 2 use of basic commands

Can't access this website can't find DNS address DNS_ PROBE_ What about started?

Wechat applet -- Summary of problems in the actual development of taro framework
随机推荐
[MCU simulation] (XXI) dB (define byte) - define byte
Note: light source selection and Application
ES6 learning notes - brother Ma at station B
Visual analysis of ncnn param file and bin model
RuntimeError_ Input type (torch.FloatTensor) and weight type (torch.cuda.FloatTensor)
D. Permutation Restoration(贪心/双指针/set)
05_服务调用Ribbon
Ncnn thread
Automatic assembly & set injection
Zabbix6.0 monitoring vcenter7.0
无线用的鉴权代码
[MCU simulation] (XX) org - set start address
Backup kubernetes backup etcd data
二分查找(leetcode704.很简单必会的)
Rewrite equals why rewrite hashcode
JDBC connection to MySQL database
Es6 notes d'étude - station B Xiao Ma Ge
[MCU simulation] (XVII) control transfer instructions - call and return instructions
Transaction and storage engine in MySQL database
After 4 years of developing two-sided meituan, we finally lost: the interview question of volatile keyword function and principle