当前位置:网站首页>Analysis and solution of JS this loss
Analysis and solution of JS this loss
2022-07-01 09:17:00 【su27_ 0101】
this Lost Cases
var obj = {
name: "Mike",
sayHi: function(){
console.log(this.name + " say Hi !");
}
}
setTimeout(obj.sayHi, 1000);// undefined say Hi
Because of the this Always point to its caller 1s after call sayHi yes window but window Not so much therefore by undefined
Solution
Nested functions
- setTimeout(function(){obj.sayHi()}, 1000);// undefined say Hi
Use bind modify this Point to
- setTimeout(obj.sayHi.bind(obj), 1000);// undefined say Hi
边栏推荐
- Graduation season, I want to tell you
- 2.2 【pytorch】torchvision.transforms
- Promise异步编程
- [ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + MQ Series + nodejs local service + MySQL storage
- OSPF - virtual link details (including configuration commands)
- TV size and viewing distance
- 序列化、监听、自定义注解
- js重写自己的函数
- 韦东山板子编译内核问题解决
- Jetson nano installs tensorflow GPU and problem solving
猜你喜欢
樹結構---二叉樹2非遞歸遍曆
Simple load balancing with Nacos
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
2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
Principles of Microcomputer - Introduction
2.2 【pytorch】torchvision.transforms
nacos服务配置和持久化配置
【电赛训练】红外光通信装置 2013年电赛真题
How to manage fixed assets efficiently in one stop?
[video game training] real topic of 2013 video game of infrared optical communication device
随机推荐
钓鱼识别app
Learning practice: comprehensive application of cycle and branch structure (II)
Niuke monthly race 22 tree sub chain
Phishing identification app
Design and manufacture of simple digital display electronic scale
【pytorch】nn. AdaptiveMaxPool2d
How to realize the usage of connecting multiple databases in idel
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
js函数arguments对象
Is it safe to dig up money and make new shares
韦东山板子编译内核问题解决
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云和Arduino的化学环境系统检测,支持钉钉机器人告警
Flink interview questions
利用闭包实现私有变量
TV size and viewing distance
Mysql8.0 learning record 17 -create table
Imitation of Baidu search results top navigation bar effect
Shell script - string
【pytorch】nn.AdaptiveMaxPool2d
Youqitong PE toolbox [vip] v3.7.2022.0106 official January 22 Edition