views:

90

answers:

3

I just want to know if the object in question, has any sub-objects within it or not. Do I really have to do THIS:

public static function getLength(o:Object):bool
{
  for (var item:* in o)
    if (item != "mx_internal_uid")
        return true;
  return false;
}

Isn't there some way to say SOMETHING LIKE: Object.hasChildren(); OR EVEN Object.childCount();

The Object in question does NOT extend the DisplayObjectContainer. It is just something like:

var Ob:Object;
Ob.SomeProp="xxx";
Ob.SomeOtherProp="zzz";

How can I know how many "entries" there are in the object. (in this case 2).

In other words, how does "for (var item:* in Ob)" know when to stop. ???

A: 

By object you mean the Object class, hence counting the properties, or the object on the displaylist, if the latter, that objects surely extends DisplayObjectContainer wich has the numChildren property

kajyr
No, I can't count on the object being an extension of DisplayObjectContainer.
Joshua
A: 

As kajyr says, if it's a DisplayObjectContainer you can check for numChildren.

If you want to check if a generic objects contains simple properties ( primitives like Number,int, uint, String, Boolean ) or complex properties (subObjects, instances of some class ) that you might regards as children to that generic object, you do the following:

var testObj:Object = {id:1,name:'DumDum'};
var testObj2:Object = {id:2,name:'NumNum',data:[1,2,3,4,5,6,7,8,9],somethingComplex:{firstName:'Num',lastName:'Num'}};

trace(isSimple(testObj).length == 0);//prints true
trace(isSimple(testObj2).length == 0);//prints false
trace(isSimple(testObj2));

function isSimple(obj:*):Array{
    var complex:Array = [];
    for(var prop in obj){
        if(!(obj[prop] is String || obj[prop] is int || obj[prop] is uint || obj[prop] is Number  || obj[prop] is Boolean)) 
            complex.push({prop: obj[prop]});
    }
    return complex;
}

If you want to get the number of members (variables associated with an object), that is easy enough to get:

var Ob:Object = {};
Ob.SomeProp="xxx";
Ob.SomeOtherProp="zzz";

trace(getMembersNum(Ob));//prints 2

function getMembersNum(obj:*):int{
    var result:int = 0;
    for(var prop in obj) result++;
    return result;
}

You would write this in your utility package/class maybe like this:

public static function get numMembers(obj:*):int{
    var result:int = 0;
    for(var prop in obj) result++;
    return result;
}

HTH

George Profenza
+4  A: 

A good class to inspect objects is the flex built in ObjectUtil. I think what you're trying to achieve would be done by using (obj is the object to analyze):

ObjectUtil.getClassInfo(obj).properties.length

But ObjectUtil.getClassInfo would be a good place too look if you're trying to analyze an object, it returns a lot of information (read more on LiveDocs).

It also has a function to check if a variable is a simple one - ObjectUtil.isSimple

Robert Bak
awesome! I didn't know about this one. Pretty Handy ! +1 Haven't used the flex framework in a while
George Profenza