JavaScript ES6 Classes - Is it an anti-pattern to call a static method to setup an instance within a class?

242 Views Asked by At

I'm creating a class that calls a method to do some processing and setup some data. I'm using ESLINT and it is warning me that I shouldn't use _ in front of a method that doesn't reference this within. This method is only meant to be used within the instance. Turning the method into a static method removes the warning. Was wondering if it is an anti-pattern to using static methods to setup an instance during instantiation or should it be kept as an instance method? Example class below. Thanks!

class AppDataArray extends BaseXmlBuilder {
  constructor(arrayOfData) {
    super();
    this.payload = this._buildAppDataArray(arrayOfData);
  }

  static _buildAppDataArray(arrayOfData) {
    const arrayOfAppData = arrayOfData.map((data) => {
      const { app, name, value } = data;
      const appData = new AppData(app, name, value);
      return appData.payload;
    });
    return arrayOfAppData;
  }
}
1

There are 1 best solutions below

1
Akash Sinha On

It depends on your case. Do you want to use this method very often both inside and outside the class ? It so then keeping it static is fine. But if you don't need this method outside class without wanting to make its object then convert it into normal member method.