Proposal: [Symbol.equals]

Jordan Harband ljharb at gmail.com
Fri Jan 18 20:23:49 UTC 2019


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_4203195586323233386_DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2>
> _______________________________________________
> 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/fd27a89a/attachment.html>


More information about the es-discuss mailing list