0

Is there a better way of doing this to dynamically:

function abcKY(a, no) {
  console.log(a + no);
}
function abcBC(a, no) {
  console.log(a - no);
}
var result_from_db = "abcKY";

eval(result_from_db)(1, 2);

I have multiple different requirements and I would like to standard like to make it configurable. So having a dynamic function call will be helpful.

1 Answer 1

1

You can put inside of an object function mappings as key-function and call it using your variable result_from_db. Check this:

var functions = {};
functions.abcKY = function(a, no) {
  console.log(a + no);
};
functions.abcBC = function(a, no) {
  console.log(a - no);
};
var result_from_db = "abcKY";
functions[result_from_db](1, 2);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.