简体   繁体   中英

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

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;
        ... 
      }
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM