开发者

Check to see if a class has a method

Is it possible to check a class to see whether it has a method or not ? Or even a particular prop开发者_开发知识库erty


var target:Object;// = some object
var name:String;// = some name
if(name in target){
    // if property/method exists
}else{
    // if property/method not exists
}


import flash.utils.describeType;
...
function methodExists(obj:Object,name:String):Boolean
{
        var desc:XML=flash.utils.describeType(obj);
        return (desc.method.(@name==name).length()>0);
}

(Note: done off the top of my head)


You can also call methods/properties from an array/lookup method such as follows. If it doesn't exist, it will be 'undefined' which also counts as 'false'.

var target:Object;// = some object

if(target["propertyName"]){
    // if property/method exists
}else{
    // if property/method not exists
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜