Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
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
about 4 years ago ยท Juan Pablo Isaza
2 answers
Answer question

0

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);
    },
  };
}
about 4 years ago ยท Juan Pablo Isaza Report

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

about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!