TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

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

1 点作者 shakiba超过 7 年前
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

earenndil超过 7 年前
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.