Actionscript 3.0 Object Equality

This weekend I began tinkering around with FlexUnit 2.0 for Actionscript 3.0. So far I am excited about the framework. It doesn’t do much, but it does the basics. When writing some of my tests I found I needed to compare two objects together. I know how to handle this in Java you would define the method equals(Object o) on the Customer object. I searched for a few hours and could find no such solution in Actionscript. Ofcourse I could make it my own standard to define this method but then frameworks like FlexUnit wouldn’t know anything about it.

Finally after searching I came across mx.utils.ObjectUtil. This object has numerous methods on it that expose the innards of objects. It has an especially powerful compare method that will transverse an object and compare each sub element testing for equality. This eliminates the need to write most equals methods on objects, but unfortunately it doesn’t get automatically used when you use the equality (==) operator.

This confuses me because the docs for equality (==) say it actually compares the value of the operand and strict equality (===) compares the memory references. Unfortunately this doesn’t seem to apply to objects as == and === have the same outcome in all my tests.

Although I am a little dissappoitned I can’t write something like:

object1 == object2

I have to instead write:

import mx.utils.ObjectUtil;, object2) == 0

I wouldn’t mind as much if FlexUnit took this into account with there assertEquals method. Currently I believe it just runs an equality (==) operation and is done. So my tests have to use the assertTrue.

Anyways I wanted to write this post to do one of two things.

  1. Save someone the trouble of searching for hours to be as dissappointed as me
  2. Have someone point out my flaws and show me the easy great Actionscript way to do equality
This entry was posted in languages, programming and tagged , . Bookmark the permalink.

19 Responses to Actionscript 3.0 Object Equality

  1. Sandro says:

    Did you find a solution in the meantime?
    The problem is serious: what about checking if a list contains an Object (using a Java “equals” like way?).

  2. thank you,
    you saved me hours ;-)

  3. Daniel Roop says:


    Glad I could help. it is a little annoying, but really at the end of the day, it is just the frameworks that need to adopt something.

  4. SJ says:

    For tests you can use something like:

    public static function assertObjectEquals(msg:String, exp:Object, act:Object):void {

    if (, act) != 0) { + “: expected , but was “);

    FlexUnit should support this more natively and no question ActionScript should add Object.equals() and have the == operator use it.

  5. Daniel Roop says:


    Thanks for the input. I agree I could right my own assert method, but ultimately I would like to see FlexUnit do this for me, as it seems to be useful outside of just my tests. Also I would prefer it to be the same assertEquals method instead of having a seperate name.

  6. Bill Shirley says:

    Definitely a design oversight. Especially when selling Flex a client/server solution – there will all kinds of “duplicate” copies of instances that are the same.

    The collectionContains functionality becomes complicated when it should be a no-brainer.

    Glad to find your post and confirm my assumptions and solutions.

  7. codecraig says:

    Is this still the case???? I hope not!

    I could create an interface that has an “equals” method and then have any classes I care about comparing implement it. Then create a special subclass of ArrayCollection (or some other collection type) that only uses objects that implement my interface (i.e. think using generics in Java to enforce this). Then override the contains method in the collection.

    Wow….totally lame.

  8. Daniel Roop says:


    I believe so and yes it should be relatively simple to implement this yourself, but ideally someone will update the Flex Libraries to include this since most of it is open source now.

  9. I’ve been developing a FlexUnit alternative called FUnit and came across this issue this weekend. Through my research, I found that FlexUnit is definitely an exception to the rule. The result of my findings can be found here:

    Pending community feedback, Assert.areEqual() will support member-wise value comparison like what has been suggested here, and NOT reference comparison. A second Assert.areSame() will be available for that purpose.

  10. Olivier says:

    Could we use the valueOf() method to check object equality ?

  11. Carlos Valdez says:


  12. Tian says:

    Thanks, this is exactly the type of stuff that confuses me, who comes from a Java background, and expeccts FlexUnit to be just like JUnit.

    Speaking of being lost in ActionScript, do you guys know if there is a HashSet-like class? About the only collection thing I found is ArrayCollection.

  13. Daniel Roop says:


    The ActionScript Object works like a Hash.

  14. NB says:

    Thank you for the explanation. ArrayCollection.contains(…) was my biggest dissapointment with actionscript.

  15. Frank says:

    Thank you for the info! It’s a pain as I come from Java background. I think 2 things are interesting: a). typeof operator helps to differentiate primitive values from objects, and then FlexUnit may leverage it to call objectEquals; b). If you provide a valueOf function, the comparison seems to be based its return value. See

  16. MikisM says:

    Frank. I thank you for the link. Seems like implementing valueOf() could work as Object.equals() in Java. Thanks again. You have just made my code much much better. :D

  17. Don Mitchell says:

    Object works more like HashMap than HashSet. There’s no great equivalence to HashSet. You could use Object w/ the Set members as the keys and the values being boolean (or anything) and then using object.deleteProperty(property) to remove them from the “Set”. Then you’d just use object.hasProperty(property) to see if it’s in the set. The iterators can iterate over the properties. Setting is object[property] = true.

  18. Cesar says:


  19. ankur says:

    Thanks you saved my time .

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>