views:

1324

answers:

3

In javascript, every object is at the same time instance and class. To do inheritance, you can use any object instance as a prototype.

In python, C++, etc.. there are classes, and instances, as separate concepts. In order to do inheritance, you have to use the base class to create a new class, which can then be used to produce derived instances.

Why did javascript go in this direction (prototype-based object orientation) ? what are the advantages (and disadvantages) of prototype-based OO with respect to traditional, class-based OO ?

thanks

+17  A: 

There are about a hundred terminology issues here, mostly built around someone (not you) trying to make their idea sound like The Best.

All object oriented languages need to be able to deal with several concepts:

(1) encapsulation of data along with associated operations on the data, variously known as data members and member functions, or as data and methods, among other things. (2) inheritance, the ability to say that these objects are just like that other set of objects EXCEPT for these changes (3) polymorphism ("many shapes") in which an object decides for itself what methods are to be run, so that you can depend on the language to route your requests correctly.

Now, as far as comparison:

First thing is the whole "class" vs "prototype" question. The idea originally began in Simula, where with a class-based method each class represented a set of objects that shared the same state space (read "possible values") and the same operations, thereby forming an equivalence class. If you look back at Smalltalk, since you can open a class and add methods, this is effectively the same as what you can do in Javascript.

Later OO languages wanted to be able to use static type checking, so we got the notion of a fixed class set at compile time. In the open-class version, you had more flexibility; in the newer version, you had the ability to check some kinds of correctness at the compiler that would otherwise have required testing.

In a "class-based" language, that copying happens at compile time. In a prototype language, the operations are stored in the prototype data structure, which is copied and modified at run time. Abstractly, though, a class is still the equivalence class of all objects that share the same state space and methods. When you add a method to the prototype, you're effectively making an element of a new equivalence class.

Now, why do that? primarily because it makes for a simple, logical, elegant mechanism at run time. now, to create a new object, or to create a new class, you simply have to perform a deep copy, copying all the data and the prototype data structure. You get inheritance and polymorphism more or less for free then: method lookup always consists of asking a dictionary for a method implementation by name.

The reason that ended up in Javascript/ECMA script is basically that when we were getting started with this 10 years ago, we were dealing with much less powerful computers and much less sophisticated browsers. Choosing the prototype-based method meant the interpreter could be very simple while preserving the desirable properties of object orientation.

Charlie Martin
Actually, Simula preceded Smalltalk, and had static type checking as well as inheritance. Simula, in fact, originated the word "Class".
John Saunders
Right, does that paragaph read as if I meant otherwise? Dahl and Nyqvist came up with "class" as the collection of things with the same method signature.
Charlie Martin
Does that change say it better?
Charlie Martin
I'd say JavaScript exactly represents CLOS object model, not even Smalltalk or Simula. CLOS was around long before Smalltalk IIRC.
Thevs
No, sorry, CLOS is from the late 80's http://dreamsongs.com/CLOS.html Smalltalk from 1980 http://en.wikipedia.org/wiki/Smalltalk and Simula with full object orientation from 1967-68 http://en.wikipedia.org/wiki/Simula
Charlie Martin
If could find that paper on "Flavour" - the precessor of CLOS, you'll find out that CLOS is around from late 70's: http://coding.derkeiler.com/Archive/Lisp/comp.lang.lisp/2004-09/0499.html
Thevs
Which is still after 1967. Flavors was first reported in AI Memo 602, which says on its face that it's providing the same functions as Smalltalk and Actor, thereby demonstrating that it is no more than contemporary with Smalltalk; it certainly doesn't predate Smalltalk.
Charlie Martin
ftp://publications.ai.mit.edu/ai-publications/pdf/AIM-602.pdf
Charlie Martin
Just read carefully, I told only about Smalltalk.
Thevs
And there was another pre-CLOS system - Common Loops. I'm not quite sure is it also a predator of Smalltalk? Then these two parts (Flavour and CLOOPS) where standartised into CLOS.
Thevs
And your reference for 1967, I will mark just as a spam. sorry for that.
Thevs
@Charle: Thanks, very useful for me.
Thevs
So, as far as I understand, the idea of class based inheritance is more proper of statically typed languages. But then why did python choose this strategy instead of the prototype based one ?
Stefano Borini
@Stephano, They're not so distinct as all that: Python, Ruby, Smalltalk use dictionaries for method lookup, and javascript and Self have classes. To some extent, you could argue that the difference is just that the prototype-oriented languages are exposing their implementations. So it's probably good not to make it into a Big Deal: it's probably more like the argument between EMACS and vi.
Charlie Martin
A: 

The Wikipedia article on Prototype-based programming may be helpful.

Steve

Steve Harrison
+1  A: 

You should check out a great book on javascript by douglas crockford. It provides a very good explanation of some of the design decisions taken by javascript creators.

One of the important design aspect of javascript is its prototypal inheritance system. Objects are first class citizens in javascript, so much that regular functions are also implemented as objects ('Function' object to be precise). In my opinion when it was originally designed to run inside a browser, it was meant to be used to create lots of singleton objects. In browser DOM, you find that window, document etc all singleton objects. Also Javascript is loosely typed dynamic language ( as opposed to say python which is strongly typed, dynamic language), as a result a concept of object extension was implemented through the use of 'prototype' property. So I think there are some pros for protytype-based OO as implemented in javascript

  1. Suitable in loosely typed environments, no need to define explicit types
  2. Makes it incredibly easy to implement singleton pattern ( compare javascript and java in this regard, and you'll know what I am talking about )
  3. Provides ways of applying a method of an object in the context of a different object, adding and replacing methods dynamically from an object etc. ( things which are not possible in a strongly typed languages )

Here are some of the cons of prototypal OO

  1. No easy way of implementing private variables. Its possible to implement private vars using crockford's wizardry using closures, but its definitely not as trivial as using private variables in say Java or C#
  2. I dont know how to implement multiple inheritance (for what its worth) in javascript yet
Amit