当前位置:网站首页>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)
},

边栏推荐
猜你喜欢
![Thesis reading [distinctive late semantic graph for video capturing]](/img/d4/4f84a73a9127fa87bb0a74c4655d15.png)
Thesis reading [distinctive late semantic graph for video capturing]

为什么一定要从DevOps走向BizDevOps?

JVM内存模型
![[research data] observation on the differences of health preservation concepts among people in 2022 - Download attached](/img/50/926cc5bce83f8b195b3e2072b656bf.jpg)
[research data] observation on the differences of health preservation concepts among people in 2022 - Download attached

After studying 11 kinds of real-time chat software, I found that they all have these functions
![pickle. Load error [attributeerror: can't get attribute 'volatile' on < module '\u main']](/img/98/c4df0747856eda262b82942eedad8f.png)
pickle. Load error [attributeerror: can't get attribute 'volatile' on < module '\u main']

ModSim基本使用(Modbus模拟器)

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

GB28181之SIP协议

如何正确使用Vertx操作Redis(3.9.4带源码分析)
随机推荐
Transaction isolation level gap lock deadlock
P2433 【深基1-2】小学数学 N 合一
Why must we move from Devops to bizdevops?
After studying 11 kinds of real-time chat software, I found that they all have these functions
[SQL optimization] the difference between with as and temporary tables
Remove line breaks from MySQL query results
JDBC中如何添加事务
pickle. Load error [attributeerror: can't get attribute 'volatile' on < module '\u main']
118. Yanghui triangle
DTD modeling
Use the uni app demo provided by Huanxin to quickly realize one-on-one chat
The key to the success of digital transformation enterprises is to create value with data
Why has instagram changed from a content sharing platform to a marketing tool? How do independent sellers use this tool?
【无标题】
OpenCV视频质量诊断----视频遮挡诊断
A brief understanding of white box encryption technology
IPv4 address, subnet mask, gateway
[research materials] iResearch tide Watching: seven major trends in the clothing industry - Download attached
Time series analysis using kibana timelion
Basic use of MySQL