Re: Objects that can stringify in multiple ways

On Wed, Mar 15, 2017 at 7:29 PM, Tab Atkins Jr. <jackalmage@gmail.com> wrote:
> If you can come up with reasonable arguments differentiating the
> different ways (probably named, in an options object?), overloading
> toString() is reasonable. Number.toString() works well this way,
> because the "different ways of printing" are just different bases,
> covered by a single simple argument integer argument.
>
> But if the different ways are relatively different, I think it's
> better to instead just add different methods to the prototype, and
> bless one of them as the "default" way.

For URLs (and hosts, which might end up as URLHost) the main
difference we might want to expose is ASCII vs Unicode display. We
could indeed have

  obj.toString({as: "unicode"})

but this seems only better than

  obj.unicode()

if it brings some other benefits (in the future?) around
stringification as otherwise it's just a pretty verbose way of
expressing the same semantic.

I'm probably overthinking it.


-- 
https://annevankesteren.nl/

Received on Wednesday, 15 March 2017 18:36:10 UTC