W3C home > Mailing lists > Public > public-script-coord@w3.org > January to March 2017

Re: Objects that can stringify in multiple ways

From: Tab Atkins Jr. <jackalmage@gmail.com>
Date: Wed, 15 Mar 2017 11:29:13 -0700
Message-ID: <CAAWBYDDvr_DRPV2CHfwj9Gnh4LH2ZnsyF4HiXpptkfgDoGF72Q@mail.gmail.com>
To: Anne van Kesteren <annevk@annevk.nl>
Cc: public-script-coord <public-script-coord@w3.org>
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

This archive was generated by hypermail 2.3.1 : Wednesday, 15 March 2017 18:30:07 UTC