没有对象的访问方法(如静态方法) [英] access method without object (like static)

查看:87
本文介绍了没有对象的访问方法(如静态方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在不创建对象或未在document.ready事件上调用构造函数的情况下调用类方法.我尝试了以下不同的选择,但没有任何效果.

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?

推荐答案

要创建静态方法(也就是说,附加到构造函数本身而不是实例的方法),请使用static:

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() {
        // ...
    }
}

然后

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

这篇关于没有对象的访问方法(如静态方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆