TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ask HN: How to Implement `toString` in JavaScript with Functional Programming?

1 pointsby shakibaover 7 years ago
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;

1 comment

earenndilover 7 years ago
Probably using a lookup table using the type as an index. I&#x27;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&#x27;s what classes and hashmaps are called in js?). If not then you&#x27;ll have to turn the type into a number somehow. Having done that, you&#x27;ll index it into an array of functions which run the appropriate stringification on the given object.