Class support for operators

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


class Complex:
    def __init__(self, re, im): = re = im
    def __add__(self, other):
        a = +
        b = +
        return Complex(a,b)
    def __str__(self):
        return '{} + {}i'.format(,
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:

class Complex{
    constructor(re,im) { = re = im
    operator add(arg) {
        // code

See also:

1 Like