How do I make a class's instances comparable and garbage collectable at the same time?

前端 未结 2 1037
一生所求
一生所求 2021-01-21 02:55

I\'m writing a class and want the instances to be comparable by <, >, ==.

For < and ><

2条回答
  •  野的像风
    2021-01-21 03:16

    You seem to be looking for hash consing, but as you experienced this cannot be implemented efficiently since JavaScript does not (yet) support weak (or soft) references.

    No, it is not possible to overwrite any operators, including ==, in JS. == will always compare two objects by reference, there's nothing you can do about it. Your best bet will be creating a compare method.

提交回复
热议问题