当前位置:网站首页>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
边栏推荐
- Design and manufacture of simple digital display electronic scale
- 2.2 【pytorch】torchvision. transforms
- Phishing identification app
- [video game training] real topic of 2013 video game of infrared optical communication device
- Redis——Lettuce连接redis集群
- Naoqi robot summary 28
- The jar package embedded with SQLite database is deployed by changing directories on the same machine, and the newly added database records are gone
- 【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
- Jetson nano installs tensorflow GPU and problem solving
- MapReduce编程基础
猜你喜欢

MapReduce编程基础

2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder

Mise en œuvre simple de l'équilibrage de la charge par nacos

I use flask to write the website "one"

NiO zero copy

OSPF - virtual link details (including configuration commands)

Principles of Microcomputer - Introduction
![2.3 [kaggle dataset - dog feed example] data preprocessing, rewriting dataset, dataloader reading data](/img/6e/d8ef618127ac492f5142f7b600266d.png)
2.3 [kaggle dataset - dog feed example] data preprocessing, rewriting dataset, dataloader reading data

Mysql 优化

Pain points and solutions of fixed assets management of group companies
随机推荐
树结构---二叉树2非递归遍历
[ESP nanny level tutorial] crazy completion chapter - Case: ws2812 light control system based on Alibaba cloud, applet and Arduino
Preparing for the Blue Bridge Cup -- bit operation
序列化、监听、自定义注解
Shell script -select in loop
Log4j log framework
【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
js原型继承仅可继承实例而非构造器
js valueOf 与 toString 区别
【pytorch学习】torch.device
Shell script case in statement
tensorrt yolov5_ trt. Py comments
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DHT11 +nodejs local service + MySQL database
【pytorch】transforms. Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
[ESP nanny level tutorial] crazy completion chapter - Case: temperature and humidity monitoring system based on Alibaba cloud, applet and Arduino
Shell script - definition, assignment and deletion of variables
Record a redis timeout
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DHT11 +NodeJs本地服务+ MySQL数据库
2.3 [kaggle dataset - dog feed example] data preprocessing, rewriting dataset, dataloader reading data
Yidian Yidong helps enterprises to efficiently manage equipment and improve equipment utilization