Self plug: I wrote a library to do this because TC39 recently abandoned the proposal to introduce it: https://www.npmjs.com/package/@nano-utils/op
User-side looks like:
import { op } from '@nano-utils/op';
class Vector {
constructor(...elems) {
this.arr = elems;
}
'operator+'(other) {
return new Vector(...this.arr.map((e, i) => e + other.arr[i]));
}
}
const a = new Vector(1, 2),
b = new Vector(3, 4);
console.log(op`${a} + ${b}`); // -> Vector(4, 6)