views:

1239

answers:

6

Why is class based OO so popular instead of prototype based OO? Do they teach the latter in schools? Javascript is object based, but people use it mostly functionally, or via frameworks.

I know that Sun has had some research on Self, but is there any other source of knowledge; preferably something that is accessible for self learned.

I found a book that contains published papers: Prototype-Based Programming: Concepts, Languages and Applications

Has anyone read it?

--

So I gave the bounty for the answer that gave me most. Still, I'm not really satisfied. I would have liked to hear much more techical answers. Maybe I didn't explain myself well.

+10  A: 

I don't know the exact reasons for this, but here are my reasons

I think this argue is the same as Dynamic vs Static, a class is the static definition of the object, that can be used easily to know what to expect from an object, it also helps tooling the languages to have proper intellisense support and documentation because you can easily know what are the different members and methods in the object, another thing is the different paradigm of having the ability to declare private members in the class that doesn't show on the object, this can't be done in the prototype paradigm.

The prototype paradigm is nice, however it lacks the ability for providing information about the methods and members in the object, which makes the tooling harder, and it also makes more sense for dynamic typing programming.

bashmohandes
right on. static languages need classes so that the compilers know what to expect/disallow. dynamic languages don't need them, so it's more convenient to use prototypes.
Javier
Agreed. Note, however, that while Javascript supports prototype-based inheritance natively, it isn't hard to implement some OO features using closures. You can get real protection of private methods and variables quite easily, for example. Dustin Diaz' book on the topic is top-notch: "Pro Javascript Design Patterns" (http://jsdesignpatterns.com/)The point stands that tooling for such dynamic languages is difficult. With such a flexible language as Javascript, it's difficult for your development environment to know what inheritance patterns you're using.
Eric Nguyen
I'd disagree on that "prototype-based lacks the ability for providing information about the methods and members in the object". In oppose to class-based, it's easily enumerated at runtime.
Thevs
Additionally, in prototype-based languages you can interpret code as data and data as a code. This is a big advantage over static class-based languages. That's why most Javascript programmes are opposing to static class introduction in proposed ECMA 4 standard.
Thevs
+2  A: 

http://en.wikipedia.org/wiki/Prototype-based_programming#Criticism explains it quite well I think.

BYK
+10  A: 

If you're looking for someone to point out the advantages/disadvantages of each as an explanation for their popularity, I think you're falling for a fallacy which is for some reason very common in technology - that popularity has something to do with some absolute measure of quality.

The truth is a lot more bland - class based OO is popular because Java uses classic OO, and Sun spent millions of dollars and a very long time building the popularity of Java - making sure people know it's used successfully in corporations, taught widely in universities, and on high school AP tests.

Prototypal/classical OO are just different ways of organizing your ideas. You can implement either one in languages that don't support it natively (Python and Java come to mind, and JavaScript on the other side).

In classical OO, you define an abstract hierarchy of classes for your objects, and then actually work with instances of those classes. In prototypal inheritance, you create a hierarchy of object instances. Although I imagine it might be a bit heretical in both camps, I don't see a reason you couldn't mix the two...

Andrey Fedorov
I'm just curious whether there is something that makes prototype based OO more challenging for developers. And mainly just would like to know what kind of problems there would be in creating complex software. I'm not saying the popularity is based on quality of paradigms, but it *could* be on of the reasons.
egaga
Thevs
Sorry, by classic, I meant class-based.
Andrey Fedorov
egaga: no, I don't think there is any difference in difficulty, just a difference in exposure. Class-based OOP is taught in all high school/college CS curricula in the US.
Andrey Fedorov
But what I was after is, is prototype based OO taught anywhere? If so, I would be interested in the material that discussed how to design programs in that way.
egaga
Prototype based is dangerous for the same reason it's powerful, you can modify anything at any time and it affects everything. It's the old argument against global variables, you never know who has their hands in what. Because of that, you must be very careful not to step on anyones toes, but it allows you to do some things that are extremely difficult or impossible in other languages (arguably for good reason).
Orclev
@egaga: I'd suggest some readings about CLOS (Common LISP Object System). It would give you right understanding about prototype-based as well as functional style of programming. Both of them are extremely applicable to Javascript. Just one fact: Almost complete LISP interpreter written in Javascript takes aroud 100 lines of code. These two languages are almost reversible to each other.
Thevs
@Orclev: That's completely unrelated. And untrue. In Ecmascript5, for example, you'll be able to "freeze" objects, guaranteeing they won't be modified later on.@egaga: I've only had experience as a Rutgers undergrad, and nobody teaches anything near prototypical inheritance there (and for good reason - there are harder things to teach in a University classroom, minor language paradigms can (and should) be learned on one's own).
Andrey Fedorov
Incidentally, classical OO isn't really about hierarchies at all, but rather (according to Alan Kay, who coined the term) about message-passing: http://lists.squeakfoundation.org/pipermail/squeak-dev/1998-October/017019.html (from a discussion about prototype-based OO).
Gracenotes
+1  A: 

I think the difference is in the power dynamic (prototyped) language gives to you. Javascript, same like LISP, gives almost unlimited power to a programmer. This power is only limited by programmer's responsibility and the level of his self-confidence. So the discussion is as old as it is - same as static typing vs. typeless. If you consider your programming power and self-discipline are strong enough - go for prototyped style.

Paraphrasing one famous saying:

Talent does what he can (read: class-based), genius does what he wants (read: prototype-based).

Thevs
But what is this power you are talking about? That is what I'm after.
egaga
The power to shoot yourself in the foot mostly. Static typing (and similarly classes) are an attempt to save the programmer from himself. It's the language deliberately making dangerous things more difficult to do. You can do amazing things simply with non-static typing, and prototype based object oriented systems, but you can likewise do some really terrible things.
Orclev
It is a power to make any imaginable constructs in your programs. It is limited only by your fantasy. Sometimes fantasies may be sick enough :)
Thevs
I really liked this answer although it is not very technical. It sums the whole thing up quite well in a stylish way =)
BYK
@Orclev: Why programmers must be "saved from themselves"? Either you are programmer, or you're not. :) You either take responsibility of your work, or you don't.
Thevs
Yes, I tend to think so as well, and I like prototype based OO, but at the same time (particularly in corporate situations) even though you might be able to write proper code, the guy next to you who you have to work with might not, in which case putting a few roadblocks in the way of awful code isn't necessarily a bad thing. Ideally of course, the moron writing the horrendous code would be fired, but well, we've seen how that goes sometimes.
Orclev
+7  A: 

The advantage of prototypal inheritance is that it potentially allows fancy metaprogramming in a straightforward way because the prototype chain is easily manipulated. This is a rather academic advantage because metaprogramming is the wrong answer 99% of the time. As an example, you could have a Javascript Key-Value Observer style data manipulation layer with a special DSL that transparently switched between a local SQLite backing when offline and a REST based server store when online via prototype swapping. I'm not sure it's the best way to do this, but it's the best I can come up with this late. It's not the sort of thing you generally want to do in project code since this sort of indirection is hell to debug once you start getting it going on multiple layers, but it's not bad when you keep it in a library.

Another less helpful advantage is that it allows you to design your own class system. I say less helpful because more or less all javascript libraries have their own slightly incompatible approach to how 'classes' are put together.

There are a lot of people replying who are mixing the inheritance model with the languages implemented in that model. The fact that javascript is dynamic and weakly typed and thus hard to tool has nothing to do with it being a prototypal language.

Karl Guertin
With Javascript's implementation, you can't switch what your prototype object is. Would it just be a change on the actual prototype object itself from SQLite to REST?
Steve Rowe
Yes. If you were doing all your trickery through a save() function, you'd replace the SQLite save() with a REST save():>>> rest_save = function(){return "REST"}>>> sql_save = function(){return "SQL"}>>> Storage = function(){/*initialization etc*/}>>> Storage.prototype.save = rest_save>>> my_obj = new Storage()>>> my_obj.save()"REST">>> Storage.prototype.save = sql_savefunction()>>> my_obj.save()"SQL"This is easily done for an entire object by using the update/merge function available in all major js libraries. Not "true" swapping but it can be useful.
Karl Guertin
I hate the lack of newlines in these comments.
Karl Guertin
+6  A: 

This question had me intrigued so I went back and read some of the original papers on the concept. It appears to have begun in the mid-1980s in the Smalltalk world but eventually became one of the founding principals of Self. Much later Javascript also adopted it.

The argument put forth in the papers is that it is easier to learn. There is really no technical benefit proposed other than learning. The papers all explain how it is just as expressive as a class-based language but much easier to learn. People naturally think about things in a concrete manner rather than in the abstract. We think of the elephant we saw at the zoo, not a generic "elephant". When we see other elephants, we classify them as differences from the first one. A prototype-based language facilitates this thinking. Think of it as programming by differential.

Is that a sufficient reason to use it in a language? Perhaps. In the 25 years since the idea first started percolating, I would argue that abstracted concepts like class-based OO has not been too hard for most people to learn. On the other hand perhaps there is a need for a blue-collar programming language (like Javascript) which is easier and this may be a way to accomplish that.

If interested, you might start with this paper about self.

Steve Rowe
Nice to see this question got some interest! Thanks :)
egaga