当前位置:网站首页>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刷题记录
- Fatal error in launcher: Unable to create process using xxx --logdir logs(tensorboard使用)
- 创业的第125天——随记
- 维特智能惯导配置
- Deep Learning - Second Time
- 压力能变成动力
- redis学习五redis的持久化RDB,fork,copyonwrite,AOF,RDB&AOF混合使用
- comp3331-9331-16s2-midterm复习
- The classification of inter-process communication (IPC) and the development of communication methods
- 聊聊对RPC的理解
猜你喜欢
Matlab矩阵(数组)元素过滤常见方法详解
Vim and copy and paste from the outside (don't need to install the plugin)
"How to kick a bad habit to read notes?
The practice of alibaba data synchronization component canal
经典面试题 之 GC垃圾收集器
代码 Revert 后再次 Merge 会丢失的问题,已解决
基于FPGA状态机的自动售货机功能实现
JS-DOM元素对象
Vulnhub靶机:GEMINI INC_ 2
Js prototype and prototype chain and prototype inheritance
随机推荐
带你系统学习MySQL索引
今天聊聊接口幂等性校验
(Nips-2015)空间变换器网络
压力能变成动力
JVM类加载机制
2022年广东省安全员A证第三批(主要负责人)操作证考试题模拟考试平台操作
【idea 报错】 无效的目标发行版:17 的解决参考
2022制冷与空调设备运行操作考试试题模拟考试平台操作
shell脚本入门
The practice of alibaba data synchronization component canal
Pytorch/TensorFlow/Numpy常用函数汇总
微信公众号后台管理
【Unity入门计划】Unity2D动画(1)-动画系统的组成及功能的使用
Research on the Application of Privacy Computing Fusion
21天学习挑战赛第一周总结
Economic Misunderstandings in the Crypto World: Is Cash a Savings?Scarcity creates value?
alibaba数据同步组件canal的实践整理
Entity到Vo的转换
SIT221 Data Structures and Algorithms课程辅导
Oops novice template Framework project guide