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

151
Views
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 answers
Answer question

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 Report

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 Report

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 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!