当前位置:网站首页>js this丢失问题分析 及 解决方案
js this丢失问题分析 及 解决方案
2022-07-01 09:14:00 【su27_0101】
this丢失案例
var obj = {
name: "Mike",
sayHi: function(){
console.log(this.name + " say Hi !");
}
}
setTimeout(obj.sayHi, 1000);// undefined say Hi
由于函数里的this 永远指向其调用者 1s 后 调用 sayHi 是 window 但window并没有那么属性 所以 为 undefined
解决方案
嵌套一层函数
- setTimeout(function(){obj.sayHi()}, 1000);// undefined say Hi
使用bind修改this指向
- setTimeout(obj.sayHi.bind(obj), 1000);// undefined say Hi
边栏推荐
- Flink面试题
- LogBack
- 【检测技术课案】简易数显电子秤的设计与制作
- Graduation season, I want to tell you
- Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
- Class loading
- Summary of reptile knowledge points
- Bird recognition app
- Shell script echo command escape character
- Daily office consumables management solution
猜你喜欢

Understanding and implementation of AVL tree

Why is the Ltd independent station a Web3.0 website!

dsPIC30F6014a LCD 方块显示

Ranking list of domestic databases in February, 2022: oceanbase regained the "three consecutive increases", and gaussdb is expected to achieve the largest increase this month

队列的实现和应用

I use flask to write the website "one"

小鸟识别APP

钓鱼识别app
![[pytorch] 2.4 convolution function nn conv2d](/img/eb/382a00af5f88d5954f10ea76343d6e.png)
[pytorch] 2.4 convolution function nn conv2d

猿人学第20题(题目会不定时更新)
随机推荐
Promise异步编程
闭包实现迭代器效果
js原型继承仅可继承实例而非构造器
JCL 和 SLF4J
js函数arguments对象
Yidian Yidong helps enterprises to efficiently manage equipment and improve equipment utilization
Principle and application of single chip microcomputer timer, serial communication and interrupt system
Flink interview questions
nacos简易实现负载均衡
Input标签的type设置为number,去掉上下箭头
Can diffusion models be regarded as an autoencoder?
美团2022年机试
Daily practice of C language - day 80: currency change
2.2 【pytorch】torchvision. transforms
dsPIC30F6014a LCD 方块显示
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + MQ Series + nodejs local service + MySQL storage
Latex插入的eps图片模糊解决方法
【pytorch】2.4 卷积函数 nn.conv2d
2.4 激活函数
Shell script -if else statement