Escher Escher - 12 days ago 5x
Javascript Question

How can I use static helper methods in the constructor (without declaring them there)?

I'm learning ES6 classes. Is there a way to make helper functions (eg for data munging) accessible to the constructor, but also elsewhere, so that I'm not typing out the function twice (as an IIFE in the constructor and a static class method too)?

Eg at the moment I'm doing because

is not callable in the constructor:

class Foo {

this._data = data;
let dimensions = function(data){
//return some dimensions
this._x = d3.scaleLinear().domain([dimensions])...

static getDimensions(someData){
//same calcs as the constructor IIFE

let dimensions = getDimensions(newData);
this.x = d3.scaleLinear().domain([dimensions]);

Is it possible/sensible to get myself a static helper method that I can use in my prototype methods and in the constructor?


Not sure what you mean. You should be able to call a static method just fine by referencing the class name. Here is a complete example showing calling a static method from one class in the constructor of another:

      <script type="text/javascript" src=""></script>
      <script type="text/javascript">
         class Defines {
            static StaticMethod(data) {
               $("#someDiv").append("static method " + data);

         class SomethingConcrete {
            constructor (data) {
      = data;

         $(function () {
            const somethingConcrete = new SomethingConcrete(3);
      <div id="someDiv">

Outputs: "static method 3"