当前位置:网站首页>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
边栏推荐
- 2.3 [kaggle dataset - dog feed example] data preprocessing, rewriting dataset, dataloader reading data
- nacos簡易實現負載均衡
- 2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
- SDN_简单总结
- Log4j log framework
- Principles of Microcomputer - Introduction
- js原型继承仅可继承实例而非构造器
- How to manage fixed assets efficiently in one stop?
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的温湿度监控系统
- Log4j 日志框架
猜你喜欢

Which method is good for the management of fixed assets of small and medium-sized enterprises?

3D打印Arduino 四轴飞行器

2.4 激活函数

NiO zero copy

Ape anthropology topic 20 (the topic will be updated from time to time)

Structure de l'arbre - - - arbre binaire 2 traversée non récursive

2.2 【pytorch】torchvision.transforms

Simple load balancing with Nacos

nacos简易实现负载均衡

【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
随机推荐
Shell脚本-特殊变量:Shell $#、$*、[email protected]、$?、$$
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
Tree structure -- binary tree 2 non recursive traversal
delete和delete[]引发的问题
【电赛训练】红外光通信装置 2013年电赛真题
TV size and viewing distance
3D打印Arduino 四轴飞行器
nacos服务配置和持久化配置
Daily practice of C language - day 80: currency change
Record a redis timeout
Pain points and solutions of equipment management in large factories
[pytorch] softmax function
如何高效拉齐团队认知
In the middle of the year, where should fixed asset management go?
JCL and slf4j
记一次redis超时
钓鱼识别app
FAQ | FAQ for building applications for large screen devices
2.3 [pytorch] data preprocessing torchvision datasets. ImageFolder
Mise en œuvre simple de l'équilibrage de la charge par nacos