Skip to content

Instantly share code, notes, and snippets.

@metanomial
Created December 14, 2019 03:52
Show Gist options
  • Save metanomial/76189a1d82602f6624837641e438d6be to your computer and use it in GitHub Desktop.
Save metanomial/76189a1d82602f6624837641e438d6be to your computer and use it in GitHub Desktop.
Math.symbol = {
ceil: Symbol('Math.symbol.ceil')
};
class ComplexNumber {
constructor(a, b) {
this.a = a;
this.b = b;
}
toString() {
return `${this.a} + ${this.b}i`;
}
[Symbol.toPrimitive](hint) {
if(hint === 'string') return this.toString();
throw TypeError('Cannot convert a ComplexNumber value to a number.')
}
[Math.symbol.ceil]() {
return new ComplexNumber(
Math.ceil(this.a),
Math.ceil(this.b),
);
}
}
const complex = ComplexNumber(4.9, 6.3);
complex.toString(); // 4.9 + 6.3i
Math.ceil(complex).toString(); // 5 + 7i
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment