1

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
0

2 Answers 2

1

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);
    },
  };
}
Sign up to request clarification or add additional context in comments.

Comments

0

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);

2 Comments

Very similar to my answer, posted some minutes later after mine :) btw you shouldn't use arrow functions like that but in the constructor.
Actually, @EliazBobadilla, I saw both answers, and preferred this one because of the working snippet. (I hope you didn't downvote on this basis).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.