Why do people use inheritance in Javascript?

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.

I like this:

Why do people use inheritance?

JavaScript makes inheritance a pain in the ass to implement, so why is it so popular among frameworks? Part of the problem is that JavaScript has always looked like a flimsy lightweight scripting language next to Java and its strongly typed kin; keen to prove that they are using a real language for big people, JavaScript developers have rushed to adopt an OO feature that was never very good in the first place. Strongly typed languages don’t use inheritance because it’s a good idea, they use it for 3 bad reasons:

1. Because they have to. Java is littered with situations where, for example, a method requires a source of input so takes an InputStream parameter. InputStream should be an interface, but it’s not, it’s a class. Therefore if you want to pass your own input into such a method you have to create a new subclass of InputStream, or the program won’t compile.

2. Some of the few places where inheritance is appropriate are found in designing large frameworks like the HTML DOM and Flash’s DisplayObject hierarchy: the kind of systems new web developers are exposed to. People see these when they are learning to program and assume that that’s how it should be done (I know I did)

3. Sheer force of habit: some http://en.wikipedia.org/wiki/Simula people http://en.wikipedia.org/wiki/Smalltalk in the 60′s decided it was a good idea, and it’s too much effort to stop.

Post external references

  1. 1
    http://berniesumption.com/software/inheritance-is-evil-and-must-be-destroyed/
Source