Reputation: 17746
Is there is an AS3 API, a Flex class or a way than what I have listed below to test if an object is empty?
This does not work on a Flex application:
var o:Object = {};
var result:Boolean = isEmpty(o); // true
var result2:Boolean = isEmpty(FlexGlobals.topLevelApplication); // true
function isEmpty(object) {
for(var i in object) { return false; }
return true;
}
UPDATE: I'm asking if there's a method in the Flash or Flex AS3 API since it doesn't work on the Application class. There are classes like ObjectUtil that I'm looking for because there are things like prototype chains and objects like the application class that don't show properties when doing a simple properties loop. Please remove the close flag so people can answer.
Upvotes: 0
Views: 239
Reputation: 52133
The reason it returns true
for the Application
class is because it doesn't have any enumerable properties. The for..in
and for..each
commands will not loop over every property of an object, only those that are enumerable, which is typically only dynamic properties like array values and object keys (but this can be changed via proxy overloading).
There's almost certainly a better way to do whatever it is you are trying to do other than to rely on a general "isEmpty" check on an object, but you could use describeType
to see if there are any properties defined on the type. You would probably want to check for accessor
and variable
nodes.
You could also ensure that the object you are dealing with is a sub-class of Object
and not simply an Object
by checking the constructor: myObject.constructor != Object
Upvotes: 1
Reputation: 1074
Can't say if this will help but there is another method for getting object properties:
import flash.utils.describeType;
trace(describeType(class));
The param is either a Class instance or a Class definition, it returns a XML with a detailed description but you need to parse it to get anything meaningful. Also, it will show only the public properties of the class. UPDATE - oh, just saw the previous answer has already described this.
Upvotes: 1