How to execute a JavaScript function when I have its name as a string
I have the name of a function in JavaScript as a string. How do I convert that into a function pointer so I can call it later?Depending on the circumstances, I may need to pass various arguments into...
View ArticleAnswer by Eli Courtwright for How to execute a JavaScript function when I...
The answer to this other question shows you how to do that: Javascript equivalent of Python's locals()?Basically, you can saywindow["foo"](arg1, arg2);or as many others have suggested, you can just use...
View ArticleAnswer by annakata for How to execute a JavaScript function when I have its...
Two things: avoid eval, it's terribly dangerous and slowsecondly it doesn't matter where your function exists, "global" -ness is irrelevant. x.y.foo() can be enabled through x.y['foo']() or...
View ArticleAnswer by Jason Bunting for How to execute a JavaScript function when I have...
Don't use eval unless you absolutely, positively have no other choice.As has been mentioned, using something like this would be the best way to do it:window["functionName"](arguments);That, however,...
View ArticleAnswer by Alex Nazarov for How to execute a JavaScript function when I have...
Just thought I'd post a slightly altered version of Jason Bunting's very helpful function.First, I have simplified the first statement by supplying a second parameter to slice(). The original version...
View ArticleAnswer by Amirali for How to execute a JavaScript function when I have its...
You just need convert your string to a pointer by window[<method name>].example:var function_name = "string";function_name = window[function_name];and now you can use it like a pointer.
View ArticleAnswer by merqlove for How to execute a JavaScript function when I have its...
There too some very helpful way.http://devlicio.us/blogs/sergio_pereira/archive/2009/02/09/javascript-5-ways-to-call-a-function.aspxvar arrayMaker = { someProperty: 'some value here', make: function...
View ArticleAnswer by Bradley Shrader for How to execute a JavaScript function when I...
One more detail on Jason and Alex's posts. I found it helpful to add a default value to context. Just put context = context == undefined? window:context; at the beginning of the function. You can...
View ArticleAnswer by Ahmet DAL for How to execute a JavaScript function when I have its...
If you want to call a function of an object instead of a global function with window["functionName"]. You can do it like;var myObject=new Object();myObject["functionName"](arguments);Example:var...
View ArticleAnswer by Coley for How to execute a JavaScript function when I have its name...
Could you not just do this:var codeToExecute = "My.Namespace.functionName()";var tmpFunc = new Function(codeToExecute);tmpFunc();You can also execute any other JavaScript using this method.
View ArticleAnswer by Wolfgang Kuehn for How to execute a JavaScript function when I have...
All the answers assume that the functions can be accessed through global scope (window). However, the OP did not make this assumption.If the functions live in a local scope (aka closure) and are not...
View ArticleAnswer by DevAshish for How to execute a JavaScript function when I have its...
This is working for me:var command = "Add";var tempFunction = new Function("Arg1","Arg2", "window."+ command +"(Arg1,Arg2)");tempFunction(x,y);I hope this works.
View ArticleAnswer by Cilan for How to execute a JavaScript function when I have its name...
To add to Jason Bunting's answer, if you're using nodejs or something (and this works in dom js, too), you could use this instead of window (and remember: eval is evil:this['fun'+'ctionName']();
View ArticleAnswer by dykstrad for How to execute a JavaScript function when I have its...
BE CAREFUL!!!One should try to avoid calling a function by string in JavaScript for two reasons:Reason 1: Some code obfuscators will wreck your code as they will change the function names, making the...
View ArticleAnswer by abhishekisnot for How to execute a JavaScript function when I have...
Surprised to see no mention of setTimeout.To run a function without arguments:var functionWithoutArguments = function(){ console.log("Executing...
View ArticleAnswer by nomæd for How to execute a JavaScript function when I have its name...
There's a very similar thing in my code.I have a server-generated string which contains a function name which I need to pass as a callback for a 3rd party library. So I have a code that takes the...
View ArticleAnswer by pmrotule for How to execute a JavaScript function when I have its...
So, like others said, definitely the best option is:window['myfunction'](arguments)And like Jason Bunting said, it won't work if the name of your function includes an...
View ArticleAnswer by Ruben Daddario for How to execute a JavaScript function when I have...
I think an elegant way of doing this is by defining your functions in a hash object. Then you can have a reference to those functions from the hash using the string. e.g.var customObject = {...
View ArticleAnswer by nils petersohn for How to execute a JavaScript function when I have...
With ES6 you could to access class methods by name:class X { method1(){ console.log("1"); } method2(){ this['method1'](); console.log("2"); }}let x = new X();x['method2']();the output would be:12
View ArticleAnswer by Magnus Smith for How to execute a JavaScript function when I have...
I can't resist mentioning another trick, which helps if you have an unknown number of arguments that are also being passed as part of the string containing the function name. For example:var...
View Article