3

A Set handles 2 empty objects as different, e.g.:

let s = new Set([ {} ]);
s.has({}); // false

This is expected, but it would be useful to compare based on their contents instead, as is possible in various other languages.

Is there any way to override the Set's internal comparison operator?

Razor
  • 27,418
  • 8
  • 53
  • 76
  • Also, this was asked 12h ago: http://stackoverflow.com/q/29272134/218196. Please use the search before you ask a new question (there are not that many es6 questions). – Felix Kling Mar 26 '15 at 18:36

2 Answers2

2

Is there any way to override the Set's internal comparison operator?

No. Set.prototype.has is defined like this:

23.2.3.7 - Set.prototype.has ( value )

The following steps are taken:

  1. Let S be the this value.
  2. If Type(S) is not Object, throw a TypeError exception.
  3. If S does not have a [[SetData]] internal slot throw a TypeError exception.
  4. Let entries be the List that is the value of S’s [[SetData]] internal slot.
  5. Repeat for each e that is an element of entries,
    1. If e is not empty and SameValueZero(e, value) is true, return true.
  6. Return false.

Therefore, Set must compare using SameValueZero comparison, and you can't change that.

Oriol
  • 274,082
  • 63
  • 437
  • 513
1

No, there isn't. The comparison used for Set instances is almost the same as the === operator, except that NaN can be put in a Set (once), meaning that NaN is treated as being === to itself for that purpose.

Pointy
  • 405,095
  • 59
  • 585
  • 614