Neeraj Verma
Neeraj Verma

Reputation: 2304

access method without object (like static)

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?

Upvotes: 0

Views: 40

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1074138

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();
});

Upvotes: 1

Related Questions