Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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) 
like image 524
raju Avatar asked Apr 20 '17 00:04

raju


People also ask

Can we use a function name as a variable name?

Thus when the function is executed, it will call the script, and the code inside the script will be executed line by line. However, the use of function names as variable names is strongly discouraged.

How do you call a function with parameters in TypeScript?

A Function is a block of code that you can use multiple times in an application. It can require one or more parameters. A list of parameters in parentheses, and a block of code in braces. To call a function, you code the function name followed by the function's parameters or arguments in parentheses.

Can I call a TypeScript function from JavaScript?

TypeScript supports the existing JavaScript function syntax for declaring and calling it within the program or the code snippet.

How do you define a function type variable in TypeScript?

The type syntax for declaring a variable in TypeScript is to include a colon (:) after the variable name, followed by its type. Just as in JavaScript, we use the var keyword to declare a variable. Declare its type and value in one statement.


1 Answers

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)
like image 93
Badacadabra Avatar answered Oct 16 '22 06:10

Badacadabra