Forum Moderators: open

Message Too Old, No Replies

Check if a string is an function call

var $strFunc = 'myFunction'; typeof($strFunc) == 'function';?

         

BlackDex

10:11 am on Nov 29, 2006 (gmt 0)

10+ Year Member



Hello there,

I Have created an function which callsback to an other function that is variable.

Currently i use eval() to check if it is an function or not.
But i wonder if there is a way to do this without the use of eval.

Example:


myFunction('myCallback', 'Nothing');

function myFunction($callback, $others) {
var $myFunctionHandler;
try {
$myFunctionHandler = eval($callback);
} catch ($expt) {
alert('callback is not a function');
return;
}
//Much other code etc.. etc..
$myFunctionHandler('Arg1', 'Arg2');
}

function myCallback($Arg1, $Arg2) {
alert($Arg1 +' - '+ $Arg2);
}

Fotiman

5:20 pm on Nov 29, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




function myFunction($callback, $others) {
if( typeof $callback!= "function" )
{
alert('callback is not a function');
return;
}
}
function myCallback($Arg1, $Arg2) {
alert($Arg1 +' - '+ $Arg2);
}
// Don't wrap the first argument with quotes
myFunction(myCallback, 'Nothing');

That is, don't pass a string... pass the actual function reference.

[edited by: Fotiman at 5:37 pm (utc) on Nov. 29, 2006]

BlackDex

9:55 am on Nov 30, 2006 (gmt 0)

10+ Year Member



The stranges thing is, that i had tryed this.
And for some reason i had some problems with it.

Now i tryed it again, and it seems to be working very nicely :).

Thx for letting me check this out again.