Settings

Theme

Classical Inheritance in JavaScript

crockford.com

31 points by anish_m 13 years ago · 8 comments

Reader

nahname 13 years ago

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.

x is a y => inheritance

x has a y => composition

Consider,

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...

A rectangle has a height

A rectangle has a width

A rectangle has a set of quadrilateral properties

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 13 years ago

Please, do not consider this article as normative.

Anyone trying to learn JS should have a look to more recent articles.

Examples:

- http://bonsaiden.github.com/JavaScript-Garden - Good insight

- https://developer.mozilla.org/en-US/docs/JavaScript - Good place to start learning, and for experienced developers a good place to contribute to

- https://duckduckgo.com/?q=javascript+inheritance+performance - Search for performance issues

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?

  • shantanubala 13 years ago

    Though I don't know of an article, this video from a while ago describes (at a high level) the "hidden classes" that represent prototypes. It was part of the original launch of Chrome about 4 years ago. Some of the related videos have more info, but basically V8 represents prototypes as classes that act as a cache for the properties, so the lookup of properties within the prototype becomes much faster than a lookup for an ordinary property of an object.

    http://www.youtube.com/watch?v=hWhMKalEicY

    EDIT - Your comment inspired me to do some additional Googling -- here's another (text-based) description that's a little clearer: https://developers.google.com/v8/design#prop_access

dlazerka 13 years ago

That article is ancient, and has drawbacks discussed in other places. Everyone interested has already seen it (its googleable by just "javascript inheritance").

brudgers 13 years ago

Obsolete, maybe. But I am a better person for learning the principles underlying Swiss Inheritance.

pyrotechnick 13 years ago

"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."

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection