[英]Use variable declared in event handler of one function in another function
i have the following class 我有以下课程
export default class{
constructor({
)} {
this.myFoo();
this.myBar();
}
myFoo() {
if (myBool){
...
}
}
myBar() {
this.myElement.on('my-trigger', (e, myBool) => {
if(myBool){
...
}
}
}
I would like to use myBool
brought in via the myBar
jquery .on()
handler in a different function myFoo
我想用myBool
通过引进myBar
jQuery的.on()
处理程序在不同的功能myFoo
In that case, You need to assign this value in scope of the class like: 在这种情况下,您需要在类的范围内分配该值,例如:
export default class{
constructor({
)} {
this.myFoo();
this.myBar();
}
myFoo() {
if (this.myBool){
//now myBool is accessible via this.myBool
...
}
}
myBar() {
var that=this;
this.myElement.on('my-trigger', (e, myBool) => {
if(myBool){
that.myBool=myBool;
...
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.