
Ask HN: How to Implement `toString` in JavaScript with Functional Programming? - shakiba
I like Functional Programming, however in JavaScript I can&#x27;t figure out how to use polymorphism with FP. For example how would you implement `toString` in JavaScript with Functional Programming?<p>Cross posted here: stackoverflow.com&#x2F;questions&#x2F;48257447&#x2F;
======
earenndil
Probably using a lookup table using the type as an index. I'm not very
familiar with javascript, but if types are first-class objects, then you can
use the type directly to index into an object (I think that's what classes and
hashmaps are called in js?). If not then you'll have to turn the type into a
number somehow. Having done that, you'll index it into an array of functions
which run the appropriate stringification on the given object.

