Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在另一个函数中使用一个函数的事件处理程序中声明的变量_Javascript_Jquery_Ecmascript 6 - Fatal编程技术网

Javascript 在另一个函数中使用一个函数的事件处理程序中声明的变量

Javascript 在另一个函数中使用一个函数的事件处理程序中声明的变量,javascript,jquery,ecmascript-6,Javascript,Jquery,Ecmascript 6,我有以下课程 export default class{ constructor({ )} { this.myFoo(); this.myBar(); } myFoo() { if (myBool){ ... } } myBar() { this.myElement.on('my-trigger', (e, myBool) => { if(myBool){ ... }

我有以下课程

export default class{
  constructor({
  )} {
    this.myFoo();
    this.myBar();
  }

  myFoo() {
    if (myBool){
      ... 
    }
  }

  myBar() {
    this.myElement.on('my-trigger', (e, myBool) => {
      if(myBool){
        ... 
      }
    }
  }
我想使用通过myBar jquery.on处理程序引入的myBool
在另一个函数myFoo中,在这种情况下,需要在类的作用域中指定此值,如下所示:

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

嘿,yadav,谢谢你的回答,我会很高兴让它成为公认的答案,只是想看看是否有替代方案/其他方法是这样命名临时变量的最佳实践?或者我应该以我的类/组件的名字命名它,以确认这对我有效。只是想知道在jsAs中是否有更干净、更简单的解决方案,根据一般用例,这种方法很好。但是,如果您的具体需求是,如两个函数何时触发,为什么需要变量?您可以选择另一种解决方案。@AkinHwan对于变量部分,我认为这是非常主观的。人们长期以来也在使用这种方法。