0

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

4

2 回答 2

3

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

于 2011-08-04T06:17:04.673 回答
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);
  }
}());
于 2011-07-29T05:39:31.507 回答