javascript - 从 jquery 范围调用到类函数

标签 javascript es6-class

我想从 Jquery 作用域(在同一个类中)调用同一个类的函数/方法。我该怎么办?

我试过:

displayTemplate.findWithAttr(action);

this.findWithAttr(action);

回应

"this.findWithAttr is not a function"

这是我的代码。

class displayTemplate{
  findWithAttr(action) {
    //to do something with action
  }
  router(action){
    if(action == "something"){
      $( "#confirm_dialog_msg" ).text("text?");
         $( "#dialog-confirm" ).dialog({
           buttons: {
             "yes": function() {
                $( this ).dialog( "close" );

               //how in this area call to "findWithAttr" function above?
                this.findWithAttr(action);

             },
             "No": function() {
               //..
             }
           }
        });
      }
  //...
  }
}

最佳答案

在进入函数的 JQuery 范围之前,像这样声明一个变量

var self = this;

然后只需执行 self.findWithAttr 就可以了。

就像:

router(action){
if(action == "something"){
  var self = this;
  $( "#confirm_dialog_msg" ).text("text?");
     $( "#dialog-confirm" ).dialog({
       buttons: {
         "yes": function() {
            $( this ).dialog( "close" );

           //how in this area call to "findWithAttr" function above?
            self.findWithAttr(action);

         },
         "No": function() {
           //..
         }
       }
    });
  }

希望这对您有所帮助。

关于javascript - 从 jquery 范围调用到类函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56378714/

相关文章:

javascript - 我可以控制 ES6 中类的创建方式吗?

javascript - 我希望通过 javascript 应用媒体查询

java - 从 Java Applet 调用 Javascript 函数

javascript - Firestore数据结构: Storing An Order in Firestore

c# - 在代码隐藏中访问动态生成的表行

javascript - 检测 ES6 类中的静态超方法

javascript - 无法导入 JavaScript 类。类型错误 : "class" is not a constructor

javascript - 元素类型无效 : expected a string (for built-in components) or a class/function React Error

java - 我怎么知道用户是否离开了 wicket 中的页面?

reactjs - React原生组件中的高阶组件(HOC)和继承有什么区别