First off, about the "name" of the function: I'm not sure if extending the Object
object is a good idea. It wasn't until recently (ECMAScript5 agian), that Object
didn't have any such "class methods", and I don't know if other scripts may trip over them.
I don't know if it's by design, but if not, your code has a serious bug: When comparing two objects with ignoreOrder == true
, then your function will return true
, when the objects have different member names, but the same member values, for example:
Object.identical({a:1, b:2}, {x: 1, y:2}, true); //returns true
Personally I find distinguishing between "sorted" and "unsorted" objects or "sorting" objects at all a bit strange, because by definition the members of an object don't have an order. For all intents and purposes {a: 1, b: 2}
and {b: 2, a: 1}
are identical and should be always handled that way. This also leads to wondering if stringify()
is the right tool to compare objects, because that wouldn't considered those two examples the same.
I think functions as defined in this StackOverflow question as much more reliable and in the spirit of how JavaScript objects are defined.