Is it possible to check a class to see whether it has a method or not ? Or even a particular property
Check to see if a class has a method
Asked Answered
well it looks like if I just do a if statement that checks whether property is defined works. but what about methods –
Hypnosis
Related: Actionscript (flex): how to know whether a property of object exists (or defined)? –
Beem
var target:Object;// = some object
var name:String;// = some name
if(name in target){
// if property/method exists
}else{
// if property/method not exists
}
+1 But note that this won't list private/protected properties/functions of the object. –
Exequatur
yea, i probably wont need it if its private or protected. thanks –
Hypnosis
This is the only correct answer as far as whether a property/method truly exists (i.e. it is enumerable), because a property can actually be defined despite holding the value 'undefined', even with strict equality. For example:
var obj:Object = {a:undefined};
will trace true for obj["a"] === undefined
, but the property "a" is still defined, as evidenced by the following loop tracing "a": for (var key:* in obj) trace(key); //traces "a"
. To get rid of the property, you'd have to call delete obj["a"]
. To look strictly for a non-null function, if(target[name] is Function)
would be best. –
Marni 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)
describeType
does not list dynamic properties. Try it on this object for instance: var target:Object = {a:123, b:"ASD", c:function():void{trace("hello");}}
- outputs the description of a plain object with just hasOwnProperty
, isPrototypeOf
and propertyIsEnumerable
. –
Exequatur No need to use 'describeType' which is very slow. –
Arum
describeType may be slow, but if you create a method to process the XML into an object-oriented structure and cache it in a dictionary by type name, you'll incur the overhead of describeType only on the very first run for any given type, after which a simple dictionary cache lookup by type name is very fast (getQualifiedClassName(instance or class) is about 3000 times faster). describeType's only catch is it doesn't capture dynamic properties, but that's by design. –
Marni
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
}
It may be better to actually check for undefined
if(target["propertyName"] == undefined)
. Otherwise, if the property is defined but set to something that evaluates to false
(ie an emtpy string, null
, 0
, or false
), it will be considered to "not exist". –
Bois If I try to use that on a function, it'll crash on me with error 1069 - even when just checking to see if it's true or false. I'm doing it with the keyword this (for syntactical research). Why? –
Notwithstanding
You must use strict equality (===) to compare
undefined
. Also, only untyped variables var x:*
can store the value undefined. Null is (==) to undefined, but not (===) to undefined, so if the property exists and is null, comparing to undefined with == will return true, despite the property existing and holding a null value. In fact, even with strict equality, a property can be defined and hold the value "undefined", which is still not the same as being undefined (i.e. running a for (var key:* in obj) loop will still return the property). Therefore, only if(key in obj)
is correct. –
Marni © 2022 - 2024 — McMap. All rights reserved.