当前位置:网站首页>Bind this of the current scope for callback functions in other cases such as timers and delayers
Bind this of the current scope for callback functions in other cases such as timers and delayers
2022-07-01 19:46:00 【Wang yuanrou】
List of articles
Front scene
mounted() {
console.log("this",this);
setTimeout(function() {
console.log("init",this);
},1000);
},

You want to access the current... In the callback function of the delayer Vue What about the instance ?
The way 1 Extra variable ( Not recommended )
mounted() {
console.log("this",this);
const that = this;
setTimeout(function() {
console.log("init",that);
},1000);
}

Extra variables created , It is not recommended to use .
The way 2 Arrow function
mounted() {
console.log("this",this);
setTimeout(
()=>{
console.log(" Arrow function ",this);
},
1000
);
},

Use the arrow function ,this Is in the parent scope this, At the same time, the arrow function arguments It is also in the parent scope arguments.
The way 3 Use bind
mounted() {
console.log("this",this);
setTimeout(function () {
console.log("bind",this);
}.bind(this),1000)
},

边栏推荐
- GC garbage collection
- [SQL optimization] the difference between with as and temporary tables
- Mo Tianlun salon | Tsinghua qiaojialin: Apache iotdb, originated from Tsinghua, builds an open source ecological road
- OpenCV视频质量诊断----视频遮挡诊断
- optaplanner学习笔记(一)案例Cloud balance
- 【无标题】
- 振弦采集模塊測量振弦傳感器的流程步驟
- Linux下安装Redis,并配置环境
- [research materials] iResearch tide Watching: seven major trends in the clothing industry - Download attached
- Time series analysis using kibana timelion
猜你喜欢

1592 例题1 国王(Sgu223 LOJ10170 LUOGU1896 提高+/省选-) 暴力思考 状压DP 01背包

Procédure de mesure du capteur d'accord vibrant par le module d'acquisition d'accord vibrant

ModSim基本使用(Modbus模拟器)

类加载机制

JS 之 常用内置类的使用

面试题篇一

Uni app product classification

118. 杨辉三角

Object creation

A brief understanding of white box encryption technology
随机推荐
Oracle物理体系结构
ffmpeg常用命令(二)
【无标题】
wireshark报文分析tcp,ftp
Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
Analysis of GetMessage underlying mechanism
torch. nn. functional. Interpolate function
JVM memory model
使用 Kibana Timelion 进行时间序列分析
通过js实现金字塔(星号金字塔,回文对称数字金字塔)
Basic knowledge of audio coding and decoding
[research materials] national second-hand housing market monthly report January 2022 - Download attached
面试题 16.16. 部分排序-双指针法
IPv4 address, subnet mask, gateway
pickle. Load error [attributeerror: can't get attribute 'volatile' on < module '\u main']
1592 例题1 国王(Sgu223 LOJ10170 LUOGU1896 提高+/省选-) 暴力思考 状压DP 01背包
What is the essential difference between Bi development and report development?
为什么一定要从DevOps走向BizDevOps?
Battery simulation of gazebo robot
2022/5/23-2022/5/30