Method Verification in Actionscript 3

ReferenceError: Error #1069: Property doSomething not found on YourObject and there is no default value. 
Look familiar?
Let’s say you want to verify an object has a method before calling that method - so flash won’t do something it shouldn’t and return a runtime error and CRASH.
SOLUTION:

public static function isMethod(object:Object, methodName:String):Boolean
{
if(object.hasOwnProperty(methodName)) return object[methodName] is Function;
return false;
}

// USAGE
var s:Sprite = new Sprite();
trace(“globaLToLocal is a method: “+isMethod(s, “globalToLocal”));
trace(“x is a method: “+isMethod(s, “x”));

// s would now, in fact, NOT do something.
if(isMethod(s, “doSomething”)) s.doSomething();

del.icio.us Slashdot Digg Facebook Technorati Google Yahoo


About this entry