-2

That's about it. I have a string = "sum" and a function named sum(a,b), how can I substitute my string to call that function;

string = "sum"
function sum(a,b)

So basically to call that function, I would want to execute it like

string(a, b)
Dion
  • 5
  • 6
  • Geez got downvoted for asking an existing question. Thanks for the help tho and pointing me to the right direction, still appreciate the help. – Dion Aug 31 '20 at 12:24

1 Answers1

1

I suggest you use dictionary, where you define all existing functions:

const funcs = {
    sum: function(a, b) {

    },
    someStuff: function(r, f) {

    }
};
//call it:
funcs["sum"](a, b);

You can simply access your function via indexing with the strings

Zer0
  • 1,580
  • 10
  • 28
  • This worked, I also just found that window[funcName](a, b) is the best solution for this. Thanks for your answer! Definitely something I can use in the future. – Dion Aug 31 '20 at 12:22
  • oh yeah, this solution is "universal", as it works also in Node.js or backend wise but in a browser you can use the window object, sure – Zer0 Aug 31 '20 at 12:36
  • Thanks! Upvoted your answer btw, just won't reflect cause of reputation. I appreciate the shared knowledge good sir. – Dion Aug 31 '20 at 12:48