Dynamic method names on JavaScript (es6) classes

I had something come up at work using a library which required classes with method names that matched exactly some magic strings being passed around. I’m not a huge fan of this pattern, but discovered a way to reduce the errors by having method names be dynamically generated – so renaming/refactoring the string wouldn’t have any unintended side effects

module.exports = {
 STATIC_METHOD: "myStaticMethodName",
 INSTANCE_METHOD: "myInstanceMethodName"
} // used throughout the codebase
const {STATIC_METHOD, INSTANCE_METHOD} = require("./methods")

class MyClass {
  static [STATIC_METHOD] () {
    // code
  }

  [INSTANCE_METHOD] () {
    // code
  }
}
const MyClass = require("./myClass")

MyClass.myStaticMethodName()

const mcInstance = new MyClass()
myInstance.myInstanceMethodName()