Created
December 14, 2019 03:52
-
-
Save metanomial/76189a1d82602f6624837641e438d6be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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