'Invoke a method (not a function) using a string

Note to dup finders: please feel free to mark this as a dup if the dup shows how to solve with classes and methods, not just functions.

I'm building a command line tool that solicits string input from the user then tries to invoke a class with matching methods and params. How do I call so that this is defined?

I have a class:

class MyClass {
  constructor() {
    this.foo = 'bar';
  }
  myMethod(param) {
    console.log(param, this.foo);  // this is undefined, based on how I invoke it
  }
}

I'd like to do this, once I get the user input...

let userInputMethod = 'myMethod';
let userInputParam = 'param';

const myInstance = new MyClass();
const method = myInstance[userInputMethod];
method(userInputParam);  // error, because I need somehow to set the context of this


Solution 1:[1]

The this context is lost, you need to bind it.

class MyClass {
  constructor() {
    this.foo = 'bar';
  }
  myMethod(param) {
    console.log(param, this.foo);
  }
}

let userInputMethod = 'myMethod';
let userInputParam = 'param';

const myInstance = new MyClass();
const method = myInstance[userInputMethod].bind(myInstance); // bind
method(userInputParam);

Or you could use arrow functions.

class MyClass {
  constructor() {
    this.foo = 'bar';
  }
  myMethod = (param) => {
    console.log(param, this.foo);
  }
}

let userInputMethod = 'myMethod';
let userInputParam = 'param';

const myInstance = new MyClass();
const method = myInstance[userInputMethod]
method(userInputParam);

Solution 2:[2]

You could bind it ?

let userInputMethod = 'myMethod';
let userInputParam = 'param';

const myInstance = new MyClass();
const method = myInstance[userInputMethod].bind(myInstance)
method(userInputParam);

But at that point why not use plain objects?

// foo.js
const foo = "bar";

const hey = {
  myMethod(param) {
    console.log(param, foo);
  },
};
//main.js
let userInputMethod = 'myMethod';
let userInputParam = 'param';

const method = hey[userInputMethod];
method(userInputParam);

If you need your class to receive data you can use closures like this ?

function makeInstance({ foo }) {
  return {
    myMethod(param) {
      console.log(param, foo);
    },
  };
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 axtck
Solution 2 Eliaz Bobadilla