简体   繁体   English

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

[英]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.

相关问题 与“标准”功能和事件处理程序使用相同的功能 - Use same function as “standard” one and as an event handler 如何在一个函数中定义两个导出处理程序并在另一个处理程序中使用一个处理程序中的变量? - How to define two export handlers within one function and use variable from one handler in another one? 变量未在事件处理程序的功能中重置 - Variable not resetting in function of event handler 通过事件处理程序将参数从一个函数发送到另一个函数 - sending arguments from one function to another through event handler 除非全局声明,否则无法从事件处理程序中的函数更新变量 - Cannot update variable from function within event handler unless its declared globally 闭包循环:绑定事件侦听器时,循环中声明的变量未正确预置到处理函数中 - closure in loop:variable declared in loop not being properly presetting into handler function, when bind an event listener 如何为一个函数使用多个事件处理程序? - How to use more than one event handler for a function? 删除另一个函数添加的事件处理程序函数 - Remove event handler function added by another function 存储一个事件处理程序中的变量以供另一事件处理程序使用 - Store variable from one event handler to be used by another event handler 我可以使用在 function 中声明的变量作为嵌套在 function 中的另一个 function 的参数吗? - can I use variable declared within a function as a parameter for another function nested inside the function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM