Operator overloading for non-value objects

Andrea Giammarchi andrea.giammarchi at gmail.com
Tue Jan 14 09:58:32 PST 2014

I think there are cases where the intent of `==` can be clear such Point2D
or Point3D comparison, as well as generic collections.

In this case I am simulating though `hasSamecontentOf` method what I think
would be nice simplifying via `==`


function Collection(entries) {
  this.push.apply(this, entries);

Collection.prototype = Object.setPrototypeOf(
    constructor: Collection,
    hasSameContentOf: function (collection) {
      return this.every(this._sameContentOf, collection);
    _sameContentOf: function (entry, i) {
      return entry === this[i];
      // or a more generic and unoptimized
      // return -1 < this.indexOf(entry);

  a = new Collection([1, 2, 3]),
  b = new Collection([1, 2, 3]),
  c = new Collection(a)

  a.hasSameContentOf(b) &&


In JS world we are "use to" compare via === so that when == is used instead
we are usually in "power user land", I don't see any conceptual shenanigans
in doing something like above code.

Long story short:

`point2Da.x === point2Db.x && point2Da.y === point2Db.y`

all over the code, if somehow necessary, would probably not look as nice as
`pointa == pointb`

Being something new not possible in ES5 or ES3, I don't even see side
effects or regressions that could affect old code: no oervload of `==`
would be present so we are back to the good old known `==` meaning.

my 2 cents

On Tue, Jan 14, 2014 at 9:06 AM, Kevin Smith <zenparsing at gmail.com> wrote:

>> In the worst case, when I don't know if I have a string or an URLUtils
>> object, I just ensure that at least one member of the equality operator is
>> stringified—and, most importantly, that it is evident from reading my code
>> that one member is stringified:
>>     a.href == url
>>     String(whatever) == url
> Is an equality operator which requires one to remember such details a
> good, dependable equality operator?  Does it really even express a
> meaningful concept of "equality"?
> Attaching more nuance on top of "==" via overloading seems like it will
> make things worse, not better.
> Brendan, can you provide a value class example which includes a
> user-defined "=="?  I'd like to see it in context.
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140114/029a6e5c/attachment.html>

More information about the es-discuss mailing list