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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Ask HN: Is there a consensus on the way to do JS inheritance?

2 点作者 ldidi将近 11 年前
I work on a project that uses classes in JS. So far I&#x27;ve already used 3 different ways to do inheritance:<p>- Subclass.prototype = new Class()<p>- Subclass.prototype = Object.create( Class.prototype )<p>- Fiber: https:&#x2F;&#x2F;github.com&#x2F;linkedin&#x2F;Fiber<p>None of those solutions were acceptable: the two firsts have issues with method overriding and super() invocation, the latter is too verbose to implement.<p>It seems that most articles on the subject link to John Resig&#x27;s Simple Javascript Inheritance: http:&#x2F;&#x2F;ejohn.org&#x2F;blog&#x2F;simple-javascript-inheritance&#x2F;, but that one is from 2008 and I was wondering if there&#x27;s one &quot;modern&quot; way to do JS inheritance and that most people agree on.<p>Is there such a thing? Is there a library that implements it?<p>Side note: Please avoid things like &quot;you&#x27;re trying to do things that JS is not meant for&quot;, I&#x27;m just trying to answer the how, not the why.

1 comment

tgriesser将近 11 年前
I think your best bet would be going with something like node.js&#x27; util.inherits, a standalone version for the browser can be found here:<p><a href="https://github.com/isaacs/inherits" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;isaacs&#x2F;inherits</a>