Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Invoking a Function by String in JavaScript

After reading many times this question and its accepted answer How to execute a JavaScript function when I have its name as a string

I tried to do it on my own. but I think its my unlucky day today that what I had tried was not working. I created already a fiddle for my testing

//the function I want to invoke by String
function CheckMe() {
 return "haha";
}
//the function that will search for the function and invoke it
function InvokeChecking(func2check) {
 var fn = func2check;
 if (typeof fn === 'function') {
 return = fn(); //invoke the function
 }
}
//the event listener ( ́ ▽ ` )ノ
$("#checker").click(function () {
 alert("event is working");
 alert(InvokeChecking("CheckMe"));
});​

http://jsfiddle.net/laupkram/qKHpu/2/

the thing I want to do is to invoke the function I declared by using string and get its return value. So I followed what I saw here in SO using the (fn==='function') argument. but it seems not working for me.

where I went wrong?

NOTE: I already check it in firebug and my function exist... Did I fall in a scoping problem? or something?

Answer*

Draft saved
Draft discarded
Cancel

lang-js

AltStyle によって変換されたページ (->オリジナル) /