Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

183
Visualizações
¿Cómo puedo acceder a una función por su nombre presente en forma de cadena en JavaScript?

Supongamos que estoy dentro de una función y he creado muchas funciones dentro de ella. De ellos quiero acceder a una función por su nombre presente en forma de cadena. ¿Cómo puedo hacer eso?

 function main(){ function a(){return "a function called"} function b(){return "b function called"} function c(){return "c function called"} function d(){return "d function called"} // Please assume that these functions do very different stuffs, so that we cannot use ternary ifs (It is just a rough example) const randomFunction = prompt("Which function do you want to call?") // Please consider that we use this user input to ask user type something from a, b, c or d. Obviously return value of prompt function would be a string // Now I want to access the function by the same name, user entered and I also want to call it. // So what can be the best approach to solve this problem, except from using "eval" or "Function" } main()
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Conviértelo en un objeto y accede con llave. es decir

Supongamos que estoy dentro de una función y he creado muchas funciones dentro de ella. De ellos quiero acceder a una función por su nombre presente en forma de cadena. ¿Cómo puedo hacer eso?

 const randomFunc = { a: function a(){return "a function called"}, b: function b(){return "b function called"}, c: function c(){return "c function called"}, d: function d(){return "d function called"}, } console.log(randomFunc.a)

a continuación, puede acceder a las funciones mediante la entrada del usuario.

about 4 years ago · Juan Pablo Isaza Relatório

0

function main(){ function a(){return "a function called"} function b(){return "b function called"} function c(){return "c function called"} function d(){return "d function called"} // Please assume that these functions do very different stuffs, so that we cannot use ternary ifs (It is just a rough example) const randomFunction = prompt("Which function do you want to call?") // Please consider that we use this user input to ask user type something from a, b, c or d. Obviously return value of prompt function would be a string // Now I want to access the function by the same name, user entered and I also want to call it. // So what can be the best approach to solve this problem, except from using "eval" or "Function" return d; } main()
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda