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)
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.
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.
TypeScript supports the existing JavaScript function syntax for declaring and calling it within the program or the code snippet.
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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With