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

154
Visualizações
Calling a typescript function named in a variable

I am working on Ionic2 app. I am calling a function from a a Page. Is it possible that I use a variable name in function call. e.g.

original code: this._userDataService.getGrandQuestionsFromServer(this.passedId, newLevel)

expected code::

this._userDataService.get`${this.questionModuleName}`QuestionsFromServer(this.passedId, newLevel) 
about 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You should be able to achieve this with bracket notation. Here is a working example:

const obj = {
  foobar(arg) {
    console.log(arg);
  }
};

const bar = "bar";
obj[`foo${bar}`]("It works!");

In your code, please try this:

this._userDataService[`get${this.questionModuleName}QuestionsFromServer`](this.passedId, newLevel)
about 4 years ago · Santiago Trujillo Relatório

0

There is a feature called Tagged template literals

A more advanced form of template literals are tagged template literals. Tags allow you to parse template literals with a function. The first argument of a tag function contains an array of string values. The remaining arguments are related to the expressions. In the end, your function can return your manipulated string (or it can return something completely different as described in the next example). The name of the function used for the tag can be named whatever you want. MDN - Template strings

You can use it to preprocess the string and generate the function call you want.

about 4 years ago · Santiago Trujillo Relatório

0

sure you can:

class A {
  callFunction(name:string) {
    this[`get${name}`](name);
  }

  getAmount(name: string) {
    alert(name);
  }
}

let a = new A();
a.callFunction('Amount');
about 4 years ago · Santiago Trujillo 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