16

There are lots of articles and posts explaining how JavaScript inheritance works, but why was JavaScript implemented using prototypal inheritance instead of classical inheritance?

I love JavaScript, so I'm not saying it's bad thing... I'm just curious.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nickytonline
  • 6,855
  • 6
  • 42
  • 76

5 Answers5

18

Here's what Brendan Eich has to say about what happened: https://brendaneich.com/2008/04/popularity/

As I've often said, and as others at Netscape can confirm, I was recruited to Netscape with the promise of "doing Scheme" in the browser. At least client engineering management including Tom Paquin, Michael Toy, and Rick Schell, along with some guy named Marc Andreessen, were convinced that Netscape should embed a programming language, in source form, in HTML.

The diktat from upper engineering management was that the language must "look like Java". That ruled out Perl, Python, and Tcl, along with Scheme.

I'm not proud, but I'm happy that I chose Scheme-ish first-class functions and Self-ish (albeit singular) prototypes as the main ingredients. The Java influences, especially y2k Date bugs but also the primitive vs. object distinction (e.g., string vs. String), were unfortunate.

Community
  • 1
  • 1
dshaw
  • 2,129
  • 2
  • 14
  • 17
9

JavaScript was originally supposed to be very much like Lisp. Even after the syntax was changed to more closely resemble C/Java, it is still Lisp in C's clothing. I think the answer lies in it's functional programming origins. In pure FP, there is no mutable state, which means no mutable objects. If you relax the rules a bit and get slightly creative, you end up with something like protypal inheritance, i.e., you can extend objects but not modify the original object. It provides the same power as inheritance and still gives you some immutability.

Finally, twist the language around to make it look like C++ and Java, and viola, you have new someFunction() and the rest is history.

noah
  • 21,289
  • 17
  • 64
  • 88
  • 1
    What do you mean by "there is no mutable state, which means no mutable objects"? – Andre Pena Jan 19 '10 at 02:38
  • @Andre: In **pure** functional programming (like Erlang for example) all variables are constants. The values they hold can't be modified after the variable is instantiated. Since we refer to objects using variables that means that objects in pure FP cannot be modified: all objects are constants. – slebetman Jan 19 '10 at 02:45
  • Alright but, in javascript, what if I do this: var a = {name="n",age=1}. a.age = 2. What am I doing? re-instantiating 'a'? – Andre Pena Jan 19 '10 at 02:49
  • 3
    @Andre My point was that JavaScript relaxes the rules and allows mutable state, but because it has its origins in FP, prototypal inheritance makes a sort of sense because it allows you to extend an object while keeping the original unchanged. – noah Jan 19 '10 at 19:07
  • 1
    lisp has mutable state; immutability has nothing to do with prototypal inheritance. – Sean McMillan Jun 28 '11 at 18:10
6

Because it was heavily influenced by Self. Both Wikipedia and the ECMA-spec mention this.

Christoph
  • 164,997
  • 36
  • 182
  • 240
1

I think it was chosen because it is easy to implement, needs no extra keywords and users don't need to understand it to be able to use the language. It is also more powerfull and flexible than class based inheritance.

It's a natural choice for a untyped language. The main advantages of class based inheritance are that it allows static typing and thus type checking and a faster table based lookup implementation.

x4u
  • 13,877
  • 6
  • 48
  • 58
1

Prototypical inheritance (with closures) allows others to do things that were never envisioned. It's the meshing of several paradigms that have come together to achieve general purpose programming.

With a prototype language, you can have "mix-ins" for your classes. You can accomplish the level of encapsulation you desire, without language specific keywords. In short, prototype languages are awesome.

I hate to say it, but JavaScript, plus some libraries, can do everything I need it to. It was subversive in its development (supposed to be subservient to Java). It has much power, in the simplest of implementations.

With enough study / playing around, you'll begin to see the advantages of it's inspiration. JavaScript is one of the few languages that "hid" it's potential intentionally. You gotta get into the politics if you want to know the "why." But, it's for this reason, that it's awesome.

pestilence669
  • 5,698
  • 1
  • 23
  • 35
  • @Pestilence, can you point me to some resources for "to do things that were never envisioned"? For example with mix-ins, I can get the methods from an object by setting my prototype to it, but I can only do this once, right? Unless I do something without needing prototypes like: - I have an object X, I want it to possess Y.doSomething - X.doSomething = Y.doSomething - (that just sets a reference to Y.doSomething though, right? What if I want it to have that independently) – ambertch Jan 21 '10 at 20:57
  • Well, emulating class inheritance is one thing you can do... But, you really needn't worry about hierarchies in a dynamically typed language. Simply clone one or more of your prototypes into a new instance. Things you can do, include: Dynamically building "classes" & instances by mixing-in methods & initializers from other "classes." Programmatically altering the interface during "construction." Even changing base "classes." Modifying all string objects to include Base64 methods is something I like to do: "foo".base64(). – pestilence669 Jan 22 '10 at 20:18
  • The prototype attribute that you're referring to is, can be considered optional, for custom classes. You can take control and bypass it entirely. – pestilence669 Jan 22 '10 at 20:25
  • This is a decent description of different ways of constructing classes in JS: http://www.ruzee.com/blog/2008/12/javascript-inheritance-via-prototypes-and-closures Take notice to how he creates and extends (in-line). I don't think at the the time JS was created, this design pattern existed. – pestilence669 Jan 22 '10 at 20:32