当前位置:网站首页>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 还是指向函数的实例
边栏推荐
猜你喜欢

Docker 链接sqlserver时出现en-us is an invalid culture错误解决方案

解决vim与外界的复制粘贴(不用安装插件)

八.数据的存储

学军中学推理社2017届招新试题

【C 数据存储详解】(1)——深度剖析整形数据在内存中的存储

深度学习-第二次

Multi-threaded ThreadPoolExecutor

【DB运营管理/开发解决方案】上海道宁为您提供提高工作便利性的集成开发工具——Orange
![[Detailed explanation of C data storage] (1) - in-depth analysis of the storage of shaping data in memory](/img/a1/b1c04d44ea74560a0be55a3b715d25.png)
[Detailed explanation of C data storage] (1) - in-depth analysis of the storage of shaping data in memory

【idea 报错】 无效的目标发行版:17 的解决参考
随机推荐
2022年G1工业锅炉司炉题库及模拟考试
MSP430如何给板子下载程序?(IAR MSPFET CCS)
【oops-framework】模板项目【oops-game-kit】使用简介
sql 使用到where和groupby时建立索引结果为啥是这样,原理是什么?
广州纸质发票再见!开住宿费电子发票即将全面取代酒店餐饮加油站发票
一言(ヒトコト)Hitokoto API
comp3331-9331-22t1-midterm复习辅导-tutorial week 5
SQL 开发的十个高级概念
PIFuHD配置记录
如何解决高度塌陷
21天学习挑战赛第一周总结
(CVPR-2017)在身体和潜在部位学习深度上下文感知特征以进行行人重识别
"How to kick a bad habit to read notes?
求和、计数的窗口函数应用
ESP32的环境配置(arduino arduino2.0 VScode platform哪个好用?)
leetcode:358. K 距离间隔重排字符串
超声三维重建总体架构
comp3331-9331-16s2-midterm复习
The ifconfig compared with IP command
df和df -lh的意思