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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Classical Inheritance in JavaScript

31 点作者 anish_m超过 12 年前

5 条评论

nahname超过 12 年前
When I need to consider inheritance in Javascript, I just use composition. I feel like I am balancing out all the times I've mistakenly used inheritance, when I should have used composition. Most of the time, inheritance is the wrong tool.<p>x is a y =&#62; inheritance<p>x has a y =&#62; composition<p>Consider,<p>A rectangle is a quadrilateral object. Okay, what is a quadrilateral object? Is this what I am concerned with or do I only care about the height and width? Maybe...<p>A rectangle has a height<p>A rectangle has a width<p>A rectangle has a set of quadrilateral properties<p>Don't get me wrong, sometimes the 'is a' relationship is the true relationship. I just find many people have a very difficult time building 'proper OO' because they are taught to stuff everything into some hierarchy that either doesn't exist or is too rigid to be useful. Think of a "tree is a plant". What defines a plant? What defines a tree? What if you had to consider a fake christmas tree? That is obviously not a plant, but it has many of the same characteristics of a tree. Should it then inherit from a tree? What if your plant can grow? Now you have a christmas tree that can grow.
yuchi超过 12 年前
Please, do not consider this article as normative.<p>Anyone trying to learn JS should have a look to more recent articles.<p>Examples:<p>- <a href="http://bonsaiden.github.com/JavaScript-Garden" rel="nofollow">http://bonsaiden.github.com/JavaScript-Garden</a> - Good insight<p>- <a href="https://developer.mozilla.org/en-US/docs/JavaScript" rel="nofollow">https://developer.mozilla.org/en-US/docs/JavaScript</a> - Good place to start learning, and for experienced developers a good place to contribute to<p>- <a href="https://duckduckgo.com/?q=javascript+inheritance+performance" rel="nofollow">https://duckduckgo.com/?q=javascript+inheritance+performance</a> - Search for performance issues<p>PS: recently I found an article (probably here on HN) describing how v8 treats prototypes, and how to not break the performance boost given by v8 assumptions. Can someone remember which one was?
评论 #4742957 未加载
评论 #4742958 未加载
dlazerka超过 12 年前
That article is ancient, and has drawbacks discussed in other places. Everyone interested has already seen it (its googleable by just "javascript inheritance").
brudgers超过 12 年前
Obsolete, maybe. But I am a better person for learning the principles underlying Swiss Inheritance.
pyrotechnick超过 12 年前
"I have been writing JavaScript for 8 years now, and I have never once found need to use an uber function. The super idea is fairly important in the classical pattern, but it appears to be unnecessary in the prototypal and functional patterns. I now see my early attempts to support the classical model in JavaScript as a mistake."