0

How would you compare 2 objects in cappuccino for equality. I have tried == and it doesn't seem to work for me.

zachzurn
  • 2,161
  • 14
  • 26

2 Answers2

3

If the object is a regular Cappuccino object and it implements the required method, you can use [objectA isEqual:objectB].

Alexander Ljungberg
  • 6,302
  • 4
  • 31
  • 37
2

Objects have first class identity. Two objects can never be equal to each other using "==" or "===".

You can have a function that determines "equality" based on iterating over the properties to see if both objects have the same named properties and those properties have the same value.

e.g.

var compareObj = (function () {
  function doCompare(a, b) {
    for (var p in a) {
      if (a.hasOwnProperty(p) && !b.hasOwnProperty(p)) {
        return false;
      }
      if (a[p] != b[p]) {
        return false;
      }
    }
    return true;
  }
  return function(a, b) {
    return doCompare(a, b) && doCompare(b, a);
  }
}());
RobG
  • 142,382
  • 31
  • 172
  • 209