Re: Objects that can stringify in multiple ways

On Wed, Mar 15, 2017 at 3:25 AM, Anne van Kesteren <annevk@annevk.nl> wrote:
> URLs represent a class of objects that can be stringified in multiple
> ways, each of which is useful. The JavaScript standard library deals
> with this situation by making toString() take an argument, as seen
> with the Number class.
>
> Is that the precedent to follow or is having some other way of
> accessing such string values reasonable too?
>
> (I feel like I've asked this question before somewhere, but I can't find it.)

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.

~TJ

Received on Wednesday, 15 March 2017 18:30:06 UTC