Proposal: [Symbol.equals]

ViliusCreator viliuskubilius416 at gmail.com
Fri Jan 18 18:31:52 UTC 2019


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 ().


---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190118/e9222837/attachment.html>


More information about the es-discuss mailing list