user defined object equality for a set in harmony (es6)

一笑奈何 提交于 2019-11-27 05:18:14

Is there a means to define equality for my objects to solve my problem?

No not really. There has been some discussion about this on the mailing list. The result is:

  • Build your own Set/Map abstraction on top of Set/Map, which would convert the objects to a primitive value according to your hashing function.
  • Wait for value objects coming in ES7.

This will do it for constructors like what you're working with.

var sameInstance = function(obj1, obj2){
    return obj2 instanceof ob1.constructor;
};

Some other types of objects you might need something different, but this should be ok for what you need.

The above is what you need in function form. To get it to work with Set you will have to inherit the Set object, and override the has method with your own has.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!