# Class support for operators

**URL:** <https://es.discourse.group/t/class-support-for-operators/110>\
**Category:** 💡 Ideas\
**Tags:** proposal\
**Created:** [October 22, 2019, 7:40am UTC](https://es.discourse.group/t/class-support-for-operators/110 "2019-10-22T07:40:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucasgruwez](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/lucasgruwez/32/143_2.png) [@lucasgruwez](https://es.discourse.group/u/lucasgruwez)\
**Post date:** [October 22, 2019, 7:40am UTC](https://es.discourse.group/t/class-support-for-operators/110/1 "2019-10-22T07:40:02Z")

</div>

One of the features that makes python such powerful language is that it allows for classes to act like built-in types through its magic methods. This allows for classes to be added together, to have specific behavior when iterated on, etc...

Example:

```javascript
class Complex:
    def __init__ (self, re, im):
        self.re = re
        self.im = im
    
    def __add__ (self, other):
        a = self.re + other.re
        b = self.im + other.im
        return Complex(a,b)
               
    def __str__ (self):
        return '{} + {}i'.format(self.re, self.im)
    
print(Complex(1,2) + Complex(3,4))
# returns 4 + 6i

```

Implementing something similar in JS could make it much more powerful when it comes to scientific calculations.

Example of how this could be implemented:

```javascript
class Complex{
    constructor(re,im) {
        this.re = re
        this.im = im
    }
    operator add(arg) {
        // code
    }
}

```

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [October 22, 2019, 6:22pm UTC](https://es.discourse.group/t/class-support-for-operators/110/2 "2019-10-22T18:22:57Z")

</div>

See also:

- [https://esdiscuss.org/topic/operator-overloading-proposal](https://esdiscuss.org/topic/operator-overloading-proposal)

- [https://esdiscuss.org/topic/operator-overloading-for-non-value-objects](https://esdiscuss.org/topic/operator-overloading-for-non-value-objects)

- [https://esdiscuss.org/topic/how-should-operator-overloading-work](https://esdiscuss.org/topic/how-should-operator-overloading-work)

- [https://esdiscuss.org/topic/is-it-planned-to-support-operator-overloading-in-es7-with-symbols-or-reserved-prototype-identifiers](https://esdiscuss.org/topic/is-it-planned-to-support-operator-overloading-in-es7-with-symbols-or-reserved-prototype-identifiers)

- [https://esdiscuss.org/topic/big-integer-big-float-and-operator-overloading-ideas](https://esdiscuss.org/topic/big-integer-big-float-and-operator-overloading-ideas)

- [https://esdiscuss.org/topic/operator-overloading-via-symbol-arithmetic](https://esdiscuss.org/topic/operator-overloading-via-symbol-arithmetic)

- [https://esdiscuss.org/topic/operator-overloading](https://esdiscuss.org/topic/operator-overloading)

- [https://esdiscuss.org/topic/operator-overloading-revisited](https://esdiscuss.org/topic/operator-overloading-revisited)
