当前位置:网站首页>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
边栏推荐
- Shell script - array definition and getting array elements
- Can diffusion models be regarded as an autoencoder?
- Jetson Nano 安装TensorFlow GPU及问题解决
- Promise asynchronous programming
- Input标签的type设置为number,去掉上下箭头
- 【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
- [ESP nanny level tutorial] crazy completion chapter - Case: chemical environment system detection based on Alibaba cloud and Arduino, supporting nail robot alarm
- Meituan machine test in 2022
- Leetcode daily question brushing record --540 A single element in an ordered array
- Understand shallow replication and deep replication through code examples
猜你喜欢

How to manage fixed assets efficiently in one stop?

PR training notes

Jeecg restart alarm 40001

nacos简易实现负载均衡

dsPIC30F6014a LCD 方块显示

2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
![[video game training] real topic of 2013 video game of infrared optical communication device](/img/ef/c2c45c1c6c24aed0a4e93101047372.png)
[video game training] real topic of 2013 video game of infrared optical communication device

2.4 activation function

nacos服务配置和持久化配置

Microcomputer principle - bus and its formation
随机推荐
[pytorch] softmax function
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + MQ系列 + NodeJs本地服务 + MySql存储
Log4j 日志框架
【pytorch】nn. AdaptiveMaxPool2d
nacos簡易實現負載均衡
微信小程序 webview 禁止页面滚动,同时又不影响业务内overflow的滚动的实现方式
3D printing Arduino four axis aircraft
nacos简易实现负载均衡
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DS18B20温度传感器 +NodeJs本地服务+ MySQL数据库
Shell脚本-变量的定义、赋值和删除
Tree structure -- binary tree 2 non recursive traversal
Log4j log framework
[ESP nanny level tutorial] crazy completion chapter - Case: ws2812 light control system based on Alibaba cloud, applet and Arduino
Principles of Microcomputer - internal and external structure of microprocessor
Graduation season, I want to tell you
Niuke monthly race 22- collect pieces of paper
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DS18B20 temperature sensor +nodejs local service + MySQL database
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
laravel postman 提交表单出现419错误。2020年7月6日记。
In the middle of the year, where should fixed asset management go?