当前位置:网站首页>this question in js
this question in js
2022-08-11 02:47:00 【Silly not silly】
问题1
$("#ad").click(function(){
setTimeout(function(){
console.log(this);
this.style.background = "pink";
} ,2000);
})
It is not possible to reachid为adThe background color of the element is pink.因为this指向window.
1、函数里面的this都指向window.
更改方法
$("#ad").click(function(){
let that = this;
setTimeout(function(){
console.log(this);
that.style.background = "pink";
} ,2000);
})
1)Record the outer layer with parametersthis的值.
$("#ad").click(function(){
setTimeout(()=>{
console.log(this);
this.style.background = "pink";
},2000)
})
2)箭头函数的this为静态.
2、以方法的形式调用时,this 是调用方法的对象
3、以构造函数的形式调用时,this 是新创建的那个对象
4、使用 call 和 apply 调用时,this 是指定的那个对象
5、箭头函数:箭头函数的 this 看外层是否有函数 如果有,外层函数的 this 就是内部箭头函数的 this 如果没有,就是 window
6、特殊情况:通常意义上 this 指针指向为最后调用它的对象.这里需要注意的一点就是 如果返回值是一个对象,那么 this 指向的就是那个返回的对象,如果返回值不是一个对象那么 this 还是指向函数的实例
边栏推荐
- 今天聊聊接口幂等性校验
- BUU刷题记录
- "How to kick a bad habit to read notes?
- A surviving spouse of the opposite sex within large turn paragraph, what for
- AI+医疗:使用神经网络进行医学影像识别分析
- redis学习五redis的持久化RDB,fork,copyonwrite,AOF,RDB&AOF混合使用
- ①In-depth analysis of CAS SSO single sign-on framework source code
- 成都纸质发票再见!开住宿费电子发票即将全面取代酒店餐饮加油站发票
- ifconfig与ip命令的比较
- 微信公众号后台管理
猜你喜欢
[4G/5G/6G专题基础-154]: 5G无线准入控制RAC(Radio Admission Control)
如何解决高度塌陷
Matlab矩阵(数组)元素过滤常见方法详解
ES6 advanced string processing new features
The practice of alibaba data synchronization component canal
【Unity入门计划】Unity2D动画(1)-动画系统的组成及功能的使用
《人生若如初见》命运多舛,人物饱满,朱亚文角色反差太惊喜
AI+医疗:使用神经网络进行医学影像识别分析
①CAS SSO单点登录框架源码深度分析
Detailed explanation of common methods of filtering matrix (array) elements in Matlab
随机推荐
shell脚本入门
0图中等 LeetCode565. 数组嵌套
压力能变成动力
GBJ3510-ASEMI家电电源用整流桥GBJ3510
数论基础-整除(编程例题)
SIT221 Data Structures and Algorithms课程辅导
OpenHarmony啃论文俱乐部-啃论文心得
代码 Revert 后再次 Merge 会丢失的问题,已解决
OpenCV创始人:开源绝不能完全免费!
[机缘参悟-66]:怎样才能让别人愿意帮你:利益共享法则、“大道”、“人性”
Mysq_Note4
The classification of inter-process communication (IPC) and the development of communication methods
【idea 报错】 无效的目标发行版:17 的解决参考
ES进阶 数组功能语法新特性详解
gRPC闭包调度器
MSP430如何给板子下载程序?(IAR MSPFET CCS)
否定语义转化层
117. 本地开发好的 SAP UI5 应用部署到 ABAP 服务器时,中文字符变成乱码的原因分析和解决方案
Realization of vending machine function based on FPGA state machine
Multi-threaded ThreadPoolExecutor