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

// stringly typed method names, used all over
module.exports = {
 STATIC_METHOD: "myStaticMethodName",
 INSTANCE_METHOD: "myInstanceMethodName"
} // used throughout the codebase
// the class module
const {STATIC_METHOD, INSTANCE_METHOD} = require("./consts")

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

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

MyClass.myStaticMethodName()

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