Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

214
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda