0

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.

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
  • possible duplicate of [Actionscript object number of properties](http://stackoverflow.com/questions/4697879/actionscript-object-number-of-properties) – Adam Harte Jan 31 '15 at 21:25
  • You are correct that you must loop over the object. Duplicate of : http://stackoverflow.com/questions/4697879/actionscript-object-number-of-properties – Adam Harte Jan 31 '15 at 21:25
  • I'm asking if there's a method in the Flash AS3 API. I already know about how to loop through and get the count of properties. Please remove the close flag. – 1.21 gigawatts Jan 31 '15 at 21:34
  • The isEmpty method does not work on the application and FlexGlobals.topLevelApplication. – 1.21 gigawatts Jan 31 '15 at 21:52
  • At first I thought this as not very reasonable question. But since you've got this reputation and the questions seems a bit too complex, I must ask - you would you ever need this thing? Just asking, seems a bit interesting :) What properties does the `topLevelApplication` has that are not visible through a loop? And Adam, I agree this is not a trivial 'show me the future' type of question - the guy stumbled upon a real weird thing.. Someone might know something about it :) – Andrey Popov Jan 31 '15 at 22:21
  • Yes. In my code I must create an object for the super class to work. So the object passes tests. But it must be assigned the application or other object for it to work. So I must check if object is brand new var o:Object = new Object() or if it's been assigned a real object used in the application. A real object will not be empty. Since an Application does not enumerate it's properties this information is valuable to me. – 1.21 gigawatts Feb 01 '15 at 00:53
  • Take a look on [How can I get list of properties in an object in Actionscript ?](http://stackoverflow.com/questions/372317/how-can-i-get-list-of-properties-in-an-object-in-actionscript). – akmozo Feb 01 '15 at 17:53

2 Answers2

1

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

1.21 gigawatts
  • 16,517
  • 32
  • 123
  • 231
Aaron Beall
  • 49,769
  • 26
  • 85
  • 103
  • This is what I was looking for, myObject.constructor != Object! Thank you. I didn't want to use describeType because I thought it was overkill but then again it caches the results. The other thing unrelated with describeType is it doesn't include all the (meta) data about a object unless you add compiler directives such as -keep-as3-metadata+=Style. However, the constructor name will do it! – 1.21 gigawatts Feb 01 '15 at 23:04
  • Just saw you edited my code from `myObject.constructor != Object` to `myObject.constructor != "[class Object]"` -- I didn't know you could edit my answer, that's interesting. Also I'm curious why you made the change? My original code was comparing to the Object constructor, your edit relies on implicit casting to string and string comparison, which is not necessary. I would edit my code back to my original, but I'm not sure what the protocol here is, since you accepted the answer... – Aaron Beall Feb 02 '15 at 02:10
  • Hmm... When I check if something is a type I use the `is` operator like, `myObject is Object`. So I thought your code was a comparison to a string like, `myObject typeof "Object"`. I also thought the constructor property was a string. Yeah, if you have a enough reputation you can edit answers. Most of the time it's helpful. I'll change it back. Here is what I had edited it to, `myObject.constructor != "[class Object]"` – 1.21 gigawatts Feb 02 '15 at 02:19
  • This reminded me of the NameUtil.getUnqualifiedClassName() method. That should return the class name as a string, `NameUtil.getUnqualifiedClassName(object)!="Object"` It calls getQualifiedClassName and splits on the "::" in the string. – 1.21 gigawatts Feb 02 '15 at 02:22
1

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.

Andrei Nikolaenko
  • 1,022
  • 1
  • 7
  • 13