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

Vulnhub靶机:GEMINI INC_ 2

js原型和原型链及原型继承

TRCX:掺杂过程分析

How to solve the problem of Tomcat booting and crashing

Detailed explanation of common methods of filtering matrix (array) elements in Matlab

Oops novice template Framework project guide

Future Trends in Vulnerability Management Programs

Detailed explanation of new features of ES advanced array function syntax

MySQL - 一条SQL在MySQL中是如何被执行的?

《如何戒掉坏习惯》读书笔记
随机推荐
Mysq_Note4
掌握这几招,轻松K.O面试官,Offer拿到吐
正式发布丨VS Code 1.70
shell脚本入门
HPSO and multi-core LSSVM based network intrusion detection
深度学习中的模型设计
四大组件---ContentResolver
①In-depth analysis of CAS SSO single sign-on framework source code
“京台高铁”亮相百度地图,真能在2035年建成吗?
[机缘参悟-66]:怎样才能让别人愿意帮你:利益共享法则、“大道”、“人性”
Pytorch/TensorFlow/Numpy常用函数汇总
添加用户报错useradd: cannot open /etc/passwd
最倒霉与最幸运
comp3331-9331-22t1-midterm复习辅导-tutorial week 5
【idea 报错】 无效的目标发行版:17 的解决参考
解决vim与外界的复制粘贴(不用安装插件)
Geogebra 教程之 04 Geogebra 小程序
否定语义转化层
CC0 vs. commercial IP: which model is better for NFTs?
基于FPGA状态机的自动售货机功能实现