Is there a way in flash to check to see if a function/method exists before calling it?
I tried something like if(typeof object.methodname == "function") and it didn't like it.
Thanks!
Ryan
Printable View
Is there a way in flash to check to see if a function/method exists before calling it?
I tried something like if(typeof object.methodname == "function") and it didn't like it.
Thanks!
Ryan
EDIT 2010: rainair just showed a situation (scroll down) where my crazy way works when dealing with private functions, and in essence might be the better way to go in general. Thanks = D
EDIT 2008: use the above, it's way better. ( can't believe i wrote the code below *face palm* ) ^_^
yep ( i think this works)
this["someString"]() 'this' can be substituted with objects as well.Code:try
{
var tempFunction:Function = this["functionString"]();
}catch (err:Error)
{
trace("lol no function");
}
works great. Thanx man! :)
you can also use in.
Code:if ("methodname" in object) object.methodname();
Hi there,
I'm trying to implement the same thing in as2
I'm trying to check if there is a function inside a movieclip called square called 'specialScale' is this possible.
I tried:
var tempFunction:Function = this.du2["specialScale"](); but it didn't seem to work.
Any help would ber greatly appreciated!
Why are you assigning the result of the function call: this['functionCall']() to a variable tempFunction that has been casted to a Function? That makes no sense.
I know what you're trying to do, but it'd be better if used like this:It may even fix your problem tbear.PHP Code:var tempFunction:Function = this['functionCall'] as Function;
tempFunction();
While looking for a better solution on the same issue I came across this pageref 1: quote: deadlock32Maybe things were slightly different then (I can't recall) but I found that both ways bear some problems. If you use senocular's suggestion, and you dealing with a private method, you are out of luck. Lets say the method onCompleteHandler() exists in a Class x and it's public:
EDIT: use the above, it's way better. ( can't believe i wrote the code below) while referring to the try catch solution on this["methodname"]
ref 2: quote: senocular:
you can also use in
if ("methodname" in object) object.methodname();
Now lets make the onCompleteHandler private!Code:trace("onCompleteHandler" in this); => TRUE
trace(this["onCompleteHandler"] != null); => TRUE
By removing the method:Code:trace("onCompleteHandler" in this); => FALSE
trace(this["onCompleteHandler"] != null); => TRUE
What worked for me was exactly what deadlock32 couldn't belive he wrote it !!Code:trace("onCompleteHandler" in this); => FALSE
trace(this["onCompleteHandler"] != null); => throws an Error
Ergo: deadlock32.s "( can't believe i wrote the code below)" wasn't that bad after all. Its the only foolproof solution as far as can see.Code:var methodExist:Boolean;
try {
methodExist = this["onCompleteHandler"] !=null;
} catch (e:Error) {
methodExist = false;
} finally {
}
Rainer