开发者

How to compare 2 objects in cappuccino for equality

How would yo开发者_运维知识库u compare 2 objects in cappuccino for equality. I have tried == and it doesn't seem to work for me.


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


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);
  }
}());
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜