javascript execute function by string name
//function to execute some other function by it's string name
function executeFunctionByName(functionName, context , args ) {
var args = Array.prototype.slice.call(arguments, 2);
var namespaces = functionName.split(".");
var func = namespaces.pop();
for(var i = 0; i < namespaces.length; i++) {
context = context[namespaces[i]];
}
return context[func].apply(context, args);
}
//my adding function, could be any function
function myAddFunction(a,b){
return a+b;
}
//execute myAddFunction from string
var c=executeFunctionByName("myAddFunction", window, 3,4); //7
Also in JavaScript:
- Create React App command
- how to learn javascript
- bullmq
- puppeteer event element change
- jquery to hide a div
- javascript array of objects
- get hash from url
- download image in nodejs
- app.listen express
- external css not working in jsp
- js string slicing
- regex for lowercase letters js
- nodemailer
- set value using javascript
- label tag alternative in react native
- javascript add div to body with class
- expressjs cheat sheet
- react ctx
- modern javascript for loop syntax
- document.print js
- create an element jquery
- how to create multiple folders at once in node js
- javascript get same elments from multiple arrays
- how to calculate average of array in javascript