access method without object (like static)

允我心安 提交于 2019-12-24 18:39:22

问题


I wanted to call a class method without creating an object or calling the constructor on the document.ready event. I tried following different options but nothing worked.

var objReportsInterface;

class ReportsInterface extends ReportBase {
  constructor() {
    super();
    objReportsInterface = this;
  }

  subCategories() {}
}

$(document).ready(function() {
  $("dropdown").on('change', function()
    objReportsInterface.subCategories();
  })
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<select id="dropdown"></select>

I was finding a static method, but getting any example related to my code. Can I use a static method in my case?


回答1:


To create a static method (which is to say, a method attached to the constructor function itself rather than to an instance), use static:

class ReportsInterface {
    // ...

    static subCategories() {
        // ...
    }
}

then

$("dropdown").on('change', function()
    ReportsInterface.subCategories();
});


来源:https://stackoverflow.com/questions/52152397/access-method-without-object-like-static

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!