Proposal: [Symbol.equals]

Isiah Meadows isiahmeadows at gmail.com
Fri Jan 18 21:39:55 UTC 2019


Yeah, I agree. I'd suggest overloading `==`, but that'd risk serious web
compat issues, especially if `null`/`undefined` aren't special-cased. I
feel an `equals` instance method added to all builtins and an
`Object.equals` attempting that method first before performing a shallow
object comparison would be the best solution.
On Fri, Jan 18, 2019 at 15:24 Jordan Harband <ljharb at gmail.com> wrote:

> It's pretty important that the meaning `===` not be able to change.
>
> On Fri, Jan 18, 2019 at 10:33 AM ViliusCreator <
> viliuskubilius416 at gmail.com> wrote:
>
>> What about having Symbol.equals?
>>
>> For example, for now this is what it does:
>>
>> ```js
>>
>> class Position {
>>     constructor(o) {
>>         this.x = o.x instanceof Number ? o.x : 0
>>
>>         this.y = o.y instanceof Number ? o.y : 0
>>
>>         this.z = o.z instanceof Number ? o.z : 0
>>     }
>> }
>> console.log(new Position({x: 10, y: 10, z: 10}) === new Position({x: 10,
>> y: 10, z: 10})
>>
>> ```
>> Output is of course, false.
>> With `Symbol.equals`, we could make it easier, instead of
>> `instance.equals(otherInstance)`.
>>
>>
>>
>> For example:
>>
>> ```js
>>
>> class Position {
>>
>>     [Symbol.equals](oIn) {
>>         return oIn.x === this.x && oIn.y === this.y && oIn.z === this.z
>>     }
>>     constructor(o) {
>>         this.x = o.x instanceof Number ? o.x : 0
>>
>>         this.y = o.y instanceof Number ? o.y : 0
>>
>>         this.z = o.z instanceof Number ? o.z : 0
>>     }
>> }
>> console.log(new Position({x: 10, y: 10, z: 10}) === new Position({x: 10,
>> y: 10, z: 10})
>>
>> ```
>> Now output would be true.
>>
>> This would save most of the time, instead of writing .equals and then
>> surrounding everything with ().
>>
>>
>> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient> Virus-free.
>> www.avast.com
>> <https://www.avast.com/sig-email?utm_medium=email&utm_source=link&utm_campaign=sig-email&utm_content=emailclient>
>> <#m_-3789658945863370968_m_4203195586323233386_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss
>>
> _______________________________________________
> 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/20190118/366229ac/attachment.html>


More information about the es-discuss mailing list