当前位置:网站首页>ES6之箭头函数
ES6之箭头函数
2022-07-16 22:10:00 【InfoQ】
箭头函数
thisarguments示例
let a = 1;
let obj = {
a: 2,
fn: () => {
setTimeout(() => {
console.log(this.a);
})
}
}
obj.fn();解题思路
- 调用了obj.fn(),我们的视线来到了 console.log(this.a);
- 在定时器的箭头函数中,并没有this,需要向上层作用域寻找
- 这时候我们把目光移到fn的箭头函数上,但是这里也没有this,再向上层作用域寻找
- 有些人到这里就会认为,this指向的是obj,this.a就是2,然而并不是。obj是个对象,它的大括号并不是一个作用域。所以fn箭头函数的上层作用域就是window
- 一部分人认为答案是1,就是认为let定义的变量在window上,然而也不是。通过window.a能发现,let并不能将变量定义在window上。
- 到了这里我们知道,this指向的是window,let定义的a不在window,那么结果就是undefined
举一反三
let a = 1;
let obj = {
a: 2,
fn: () => {
setTimeout(() => {
console.log(a);
})
}
}
obj.fn();边栏推荐
- 5.< tag-动态规划和完全背包问题>lt.70. 爬楼梯 || 进阶版 + lt.322. 零钱兑换 + lt.139. 单词拆分 + lt.279.完全平方数 dbc
- E. Split Into Two Sets
- Hibench generates benchmark data sets [wordcount as an example]
- 每日一题:回文链表(剑指off027)
- matplotlib.pyplot使用(subplots,gray
- Common password regular expressions
- 账号创建+登录+联系表单代码
- Pytest interface automation test framework | Interface Association
- About the use of go modules environment construction and package management tools
- 从编译器对指令集的要求看API设计原则
猜你喜欢

OpenPose:使用部分亲和场的实时多人 2D 姿势估计

Pay attention to using yyyy-mm-dd in the code!

9. Learn to check GC logs

5.< tag-动态规划和完全背包问题>lt.70. 爬楼梯 || 进阶版 + lt.322. 零钱兑换 + lt.139. 单词拆分 + lt.279.完全平方数 dbc

每日一题:回文链表(剑指off027)

Redis distributed cache - Data Persistence

Learn more about Arduino steering gear control library file servo h

回到顶部,滚动条慢慢回到顶部

Wechat applet_ 14. Creation and reference of components

Core principle of buffer pool
随机推荐
Pytest interface automation testing framework | pytest combines secondary packaging to realize interface automation
nodeJS中使用promise实现文件读取、写入的案例
Pytest interface automated testing framework | project practice (pytest+allure+ data driven)
Applet: the picker view selector scrolls quickly. When confirming, the "value displays an error."“
Introduction to OOM
go语言实现登录注册收藏相关工具和教程链接
Record of SQL questions of Niuke this week
从零开始配置 vim(4)——键盘映射的一些技巧
yarn /nmp全局安装后,命令不生效
单行文本 超出部分省略号,多行文本超出部分省略号,指定多行
P5js coffee cup steaming JS special effect
In depth explanation of MySQL index
Use of MySQL index
MySQL locking mechanism
Une seule ligne de texte dépasse l'ellipse partielle, plusieurs lignes de texte dépassent l'ellipse partielle, spécifiez plusieurs lignes
Verify that the two strings are consistent
Execution plan of SQL statement
Wechat applet_ 17. Slot
响应式表单样式透明设计
大雪菜味题解--P4017 最大食物链计数