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.
// 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 |
Prev | Home | Next |
ISO 639 | Up | isODigit() |
JavaScript Programmer's Reference, Cliff Wootton Wrox Press (www.wrox.com) Join the Wrox JavaScript forum at p2p.wrox.com Please report problems to support@wrox.com © 2001 Wrox Press. All Rights Reserved. Terms and conditions. |