当前位置:网站首页>js中的this问题
js中的this问题
2022-08-11 02:42:00 【糊涂不是傻】
问题1
$("#ad").click(function(){
setTimeout(function(){
console.log(this);
this.style.background = "pink";
} ,2000);
})
此时达不到把id为ad的元素背景色置为粉色。因为this指向window。
1、函数里面的this都指向window。
更改方法
$("#ad").click(function(){
let that = this;
setTimeout(function(){
console.log(this);
that.style.background = "pink";
} ,2000);
})
1)用参数记录外层this的值。
$("#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 还是指向函数的实例
边栏推荐
- Js prototype and prototype chain and prototype inheritance
- OpenCV创始人:开源绝不能完全免费!
- google搜索技巧——程序员推荐
- ARM development (4) How to read the chip manual for novice Xiaobai, bare metal driver development steps and pure assembly to achieve lighting, assembly combined with c lighting, c to achieve lighting
- 数据的存储(下)——浮点型在内存中的存储
- The ifconfig compared with IP command
- 架构篇(二)架构的复杂度来源
- Matlab矩阵(数组)元素过滤常见方法详解
- How to solve the problem of Tomcat booting and crashing
- MSP430如何给板子下载程序?(IAR MSPFET CCS)
猜你喜欢

Salesforce解散中国团队,什么样的CRM产品更适合中国人

测试3年,开口就要25k?面试完最多给15k...

Section 4-6 of the first week of the second lesson: Appreciation of medical prognosis cases + homework analysis
![报错处理:org.xml.sax.SAXParseException: 不允许有匹配 “[xX][mM][lL]“ 的处理指令目标](/img/35/650c92ac4c5fc2d5826f3216a09e65.png)
报错处理:org.xml.sax.SAXParseException: 不允许有匹配 “[xX][mM][lL]“ 的处理指令目标

ROS源代码阅读(1)

【idea 报错】 无效的目标发行版:17 的解决参考

gRPC闭包调度器

一次简单的 JVM 调优,拿去写到简历里

经典面试题 之 GC垃圾收集器

关于地图GIS开发事项的一次实践整理(上)
随机推荐
The ifconfig compared with IP command
2022年广东省安全员A证第三批(主要负责人)操作证考试题模拟考试平台操作
年薪30W,BAT抢着要,懂面试技巧的测试人究竟多吃香?
深度学习-第二次
隐私计算融合应用研究
报错处理:org.xml.sax.SAXParseException: 不允许有匹配 “[xX][mM][lL]“ 的处理指令目标
聊聊对RPC的理解
[Detailed explanation of C data storage] (1) - in-depth analysis of the storage of shaping data in memory
CSAPP Data Lab
BUU brushing record
维特智能惯导配置
数论基础-整除(编程例题)
comp3331-9331-16s2-midterm复习
Multi-threaded ThreadPoolExecutor
Geogebra 教程之 04 Geogebra 小程序
[4G/5G/6G专题基础-154]: 5G无线准入控制RAC(Radio Admission Control)
数据存储全方案----详解持久化技术
四大组件---ContentResolver
网络安全笔记第四天day4(kali基本操作)
经典面试题 之 GC垃圾收集器