isObjectEqual() (Simulated functionality)

Compare the properties of two objects for equality.

Property/method value type:Boolean primitive

This function tests two objects for equality. This is not the same as identity. Two objects may be deep copies of one another but still be distinctly different objects. If they have the same properties because they are both of the same class, you can compare their enumerable properties for equality.

The example code is fairly simple and could be enhanced in several ways. For example, you should possibly enumerate the second object as well if the first pass proves to yield a true value. This would ensure that if the second object has additional attributes, the test will properly yield a Boolean false result.

You could also test for object type and return false. This would allow the test to be applied to dissimilar objects and still yield a meaningful result.

The result will be true if all enumerable properties are equal and false if any of them are not.

Example code:

   // Test objects for equality
   function isObjectEqual(anObject1, anObject2)
   {
   
      for(myProp in anObject1)
      {
         if(anObject1[myProp] != anObject2[myProp])
         {
            return false;
         }
      }
      return true;
   }

See also:Element.currentStyle