11

Possible Duplicate:
prototype based vs. class based inheritance

This question came up at work the other day - what's the difference between a class-based language like Python, and a prototype-based language like Javascript? Aside from differing approches, both ways seem very similar and we struggled to find something that a class-based language could do that a prototype-based language couldn't, or vice-versa.

Can anybody elaborate or go into any detail on how they differ fundamentally?

I haven't found much online about the differences, just sites that show you how to accomplish one with the other (such as this: Simulating classes with prototypes in JavaScript)

Any enlightenment appreciated!

Community
  • 1
  • 1
Ben
  • 6,687
  • 2
  • 33
  • 46
  • Might be helpful: http://stackoverflow.com/questions/816071/prototype-based-vs-class-based-inheritance – kanaka Jul 23 '12 at 21:13
  • JavaScript doesn't implement interfaces, nor can it natively enforce it. This limits things like [SPI](http://en.wikipedia.org/wiki/Service_provider_interface). – Petah Jul 23 '12 at 21:16
  • See also http://stackoverflow.com/questions/7707176/demonstrating-javascript-inheritance-benefits-in-relation-to-a-class-based-model, http://stackoverflow.com/questions/6266690/what-are-patterns-you-could-use-with-prototype-inheritance-that-you-cannot-with – Bergi Jul 23 '12 at 21:16
  • knowing JavaScript's history will explain why it's prototypal at the same time class-based. – Joseph Jul 23 '12 at 21:17
  • 1
    @Petah: That is actually more of a dynamic vs static typing thing. Dynamic class-based languages also have the same "problem". – hugomg Jul 23 '12 at 21:19
  • @Joseph Eich stated that he'd been in big trouble if he'd added classes to Javascript; after all it's initial design was for it to be Java's dumb little brother which shouldn't be used for serious programs (hey don't look at me, I'm roughly quoting; that's from coders at work). One problem that should be noted is the pretty problematic fact of optimizing prototype based approaches.. – Voo Jul 23 '12 at 22:29

4 Answers4

6

Check out this article. It is a detailed article discussing the differences between class-based and prototype-based languages.

Copy of the table summarizing the differences:

Class-based (Java)

  • Class and instance are distinct entities.
  • Define a class with a class definition; instantiate a class with constructor methods.
  • Create a single object with the new operator.
  • Construct an object hierarchy by using class definitions to define subclasses of existing classes.
  • Inherit properties by following the class chain.
  • Class definition specifies all properties of all instances of a class. Cannot add properties dynamically at run time.

Prototype-based (JavaScript)

  • All objects are instances.
  • Define and create a set of objects with constructor functions.
  • Same.
  • Construct an object hierarchy by assigning an object as the prototype associated with a constructor function.
  • Inherit properties by following the prototype chain.
  • Constructor function or prototype specifies an initial set of properties. Can add or remove properties dynamically to individual objects or to the entire set of objects.
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
jeff
  • 8,300
  • 2
  • 31
  • 43
  • You really could've copied the tables. Formatting text as pixel data was never a good idea. – Bergi Jul 23 '12 at 21:24
  • Thanks Jeff, quite helpful. But Python differs from Java in the way that classes are also first-class objects, and you can also add properties dynamically at run time - so how does this differ from Javascript's prototype approach? – Ben Jul 23 '12 at 21:24
  • @Bergi - SO doesn't even allow table markup. I personally like the images better than trying to mock a table in text. – jeff Jul 23 '12 at 21:27
  • Of course, in more "pure" OO languages, classes are themselves objects. For instance, Python has "classes", but you can assign properties dynamically (well, unless it's a built-in or extension class or `__slots__` is being used), and Python classes are, for the most part, themselves instances of class `type`. So there's a bit of a spectrum, so to speak. – JAB Jul 23 '12 at 21:27
  • @Ben - I've never used Python, but you can also add properties dynamically to Javascript objects. – jeff Jul 23 '12 at 21:30
  • @JAB - that's my question! How does Python's classes-as-objects approach differ to Javascript's prototype approach? – Ben Jul 23 '12 at 21:30
  • @jeff - yes, which you can do in Python too, so I guess I should have worded my question better. How does Python's particular approach differ? (as opposed to Java, which is clearly quite different) – Ben Jul 23 '12 at 21:31
  • @Ben - I'm afraid I don't know, as I've never used Python. I bet that if you google "Differences between Python and Javascript," you'll find something on that, though. – jeff Jul 23 '12 at 21:36
  • @Ben Well it's the old [metaclass](http://en.wikipedia.org/wiki/Metaclass) concept, nothing new there. Lots of languages have it and as I understand it the concept is much more prevalent in Ruby (never written a single line in it, so that's hearsay). The "inventor" of the concept would be smalltalk as usual though. – Voo Jul 23 '12 at 22:35
  • @Voo I understand that it's because of metaclasses, but what I'm asking is how that's different to prototypes? – Ben Jul 24 '12 at 06:50
5

It seems like you're familiar with the actual languages, so you know what the difference is, right? I guess you're asking about the differences at a deeper, maybe more "philosophical", level.

Class-based languages tend to work from the top down, general to particular. The classic example would be where you define a 'Vehicle' class, and then subclasses like 'Car', 'Train'.

A prototype-based language would instead tend to start with the particular, in fact start with an instance of the particular and modify that.

I like this: http://steve-yegge.blogspot.ie/2008/10/universal-design-pattern.html

In the end it's not a question of if you can do inheritance in JS or whether there is something that you can do in one language but not the other. It's a deep difference in their ways of approaching problem solving. For a particular problem a good idiomatic solution that made best use of the language's features would probably be quite different in a prototype-based language from one in a class-based language.

scytale
  • 12,346
  • 3
  • 32
  • 46
  • In a prototype language you can start from a particular and then generalize but you can start general and go particular as well. In class-based languages there is less flexibility [however, Python & Ruby & Smalltalk all allow their classes to have the fexibility of prototype languages through stuff like __getattr__, method_missing, class decorators, open classes, changing types of objects, adding methods to objects, deleting methods from classes, etc...] – aoeu256 Jul 09 '19 at 15:26
1

The JavaScript guide of MDN has some good points, take a look: https://developer.mozilla.org/en/JavaScript/Guide/Details_of_the_Object_Model

davidbuzatto
  • 9,207
  • 1
  • 43
  • 50
0

Well, they're both (usually) Turning Complete. :)

Classes are about enforcing encapsulation, at the expense of some (or a lot of) run-time extensibility. Prototypes encourage encapsulation and allow almost unlimited run-time extension.

Seriously, prototype-based OO makes it very easy -- some would say dangerously easy -- to dynamically extend/derive from an object. Prototype languages therefore do not coexist easily with static typing. So if you like how compilers catch type errors, you will almost certainly be excluding prototype-based languages.

There's actually a very good discussion here: http://yehudakatz.com/2011/08/12/understanding-prototypes-in-javascript/ although there are others which are just as good I liked that one.

Carlos
  • 1,470
  • 10
  • 18