当前位置:网站首页>js中this的默认指向及如何修改指向 2021.11.09
js中this的默认指向及如何修改指向 2021.11.09
2022-06-28 11:20:00 【即将成为大佬的小梦】
各场景下 this 的默认指向:
- 在普通函数中指向 window
- 在事件函数中指向 事件源
- 在定时器中指向是 widnow
- 在自执行函数中指向 window
- 在对象中指向 对象
- 在箭头函数中是没有this指向的 往上级查找
修改this指向的方法:
1)函数名. call(要改变的指向,参数1,参数2,.......);
2)函数名. apply(要改变的指向,【参数1,参数2,.......】);
3)函数名. bind(要改变的指向,参数1,参数2,.......) ;
区别:
call和apply的区别:传参时的格式不一样,apply要以数组形式。
bind和call/apply的区别:call/apply侧重借用,一定有个调用的过程,本质也是修改this,而bind就
是用来修改this,不调用(可手动调用,后面加() )
bind的意义:一些场景下,函数并不是立即执行,而是某个时刻被回调的 这时候bind更合适,如定
时器函数。
边栏推荐
猜你喜欢

网页提示此站点不安全解决方案

JS基础3

JS基础8

Metersphere implements UI automation elements that are not clickable (partially occluded)

vsftpd服务的部署及优化

Making and using of static library
This Exception was thrown from a job compiled with Burst, which has limited exception support. 报错

Debug debugging in katalon

获取系统当前日期

行业分析| 快对讲,楼宇对讲
随机推荐
Katalon global variable is referenced in testobject
Recommended practice sharing of Zhilian recruitment based on Nebula graph
买股票在中金证券经理的开户二维码上开户安全吗?求大神赐教
获取系统当前日期
JS基础8
soapui的菜鸟教程
setInterval、setTimeout和requestAnimationFrame
使用API快捷创建ECS
毕业季,给初入社会的你一些建议
【SemiDrive源码分析】【X9芯片启动流程】32 - DisPlay模块分析 - RTOS侧
[practice] appium settings app is not running after 5000ms
将浏览器中的文件 url转换为File流
2022 open source software security status report: over 41% of enterprises do not have enough confidence in open source security
还在用 SimpleDateFormat 做时间格式化?小心项目崩掉!
[semidrive source code analysis] [x9 chip startup process] 32 - play module analysis - RTOS side
Which broker is safer and more convenient to open an account for Oriental Fortune mobile stock?
TiDB v6.0.0 (DMR) :缓存表初试丨TiDB Book Rush
Spatial-Temporal时间序列预测建模方法汇总
Fancy features and cheap prices! What is the true strength of Changan's new SUV?
培训通知|2022年境外中资企业机构及人员疫情防控和安全防范专题培训通知