RE: Objects that can stringify in multiple ways

From: Anne van Kesteren [mailto:annevk@annevk.nl] 

> 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?

IMO both are reasonable. I guess my gut feeling is that adding an option to toString makes sense when this is a very secondary thing that most code won't use (like non-base-10 serializations for numbers). Whereas adding a separate method makes sense when that particular serialization is a first-class domain concept.

I'd also say that if you were to add an option to toString, ideally it'd be a single enum or other parameter, instead of an options bag. If your serialization configuration is complex enough that an options bag has to get involved them probably splitting into separate methods would be better.

Received on Wednesday, 15 March 2017 20:53:28 UTC