当前位置:网站首页>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
边栏推荐
- 2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
- Installation and use of NoSQL database
- Flink面试题
- Daily office consumables management solution
- Tree structure -- binary tree 2 non recursive traversal
- Can diffusion models be regarded as an autoencoder?
- Redis -- lattice connects to redis cluster
- TV size and viewing distance
- js valueOf 与 toString 区别
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DHT11 +NodeJs本地服务+ MySQL数据库
猜你喜欢

【电赛训练】红外光通信装置 2013年电赛真题

樹結構---二叉樹2非遞歸遍曆

An overview of the design of royalties and service fees of mainstream NFT market platforms

Tree structure -- binary tree 2 non recursive traversal

Bird recognition app

nacos簡易實現負載均衡

Installing Oracle EE

Jetson Nano 安装TensorFlow GPU及问题解决

3D打印Arduino 四轴飞行器

FAQ | FAQ for building applications for large screen devices
随机推荐
Mise en œuvre simple de l'équilibrage de la charge par nacos
Phishing identification app
利用闭包实现私有变量
Reproduced Xray - cve-2017-7921 (unauthorized access by Hikvision)
Log4j 日志框架
【pytorch】softmax函数
Record a redis timeout
【pytorch】nn. AdaptiveMaxPool2d
ES6-const本质与完全不可改实现(Object.freeze)
Learning practice: comprehensive application of cycle and branch structure (II)
Input标签的type设置为number,去掉上下箭头
nacos簡易實現負載均衡
I use flask to write the website "one"
Meituan machine test in 2022
2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
Key points of NFT supervision and overseas policies
Shell script - array definition and getting array elements
Shell script -for loop and for int loop
Exception handling of classes in C #
Nacos service configuration and persistence configuration