Sunday nitpickery: class-based vs prototype-based OO
This Wikipedia article on ActionScript claims:
“Two important features of ActionScript that distinguish it from later versions are its loose type system and its reliance on prototype-based inheritance. [...] Prototype-based inheritance is the ActionScript 1.0 mechanism for code reuse and object-oriented programming. Instead of a class keyword that defines common characteristics of a class, ActionScript 1.0 uses a special object that serves as a “prototype” for a class of objects. All common characteristics of a class are defined in the class‘s prototype object and every instance of that class contains a link to that prototype object.”
and (emphasis mine):
“[...] ActionScript 2.0 featured compile-time type checking and class-based syntax, such as the keywords class and extends. (While this allowed for a more flexible object-oriented programming approach, the code would still be compiled to ActionScript 1.0 bytecode, allowing it to be used on the preceding Flash Player 6 as well. In other words, the class-based inheritance syntax was a layer on top of the existing prototype-based system.)”
I’m not getting that. Prototype-based OO is, by its very nature, more flexible than class-based. It doesn’t even matter if the class-based OO flavor has bells and whistles like metaclasses, multiple inheritance, class methods, or whatever. All of these become redundant with prototype-based OO. So how can they claim that the addition of class-based syntax allows for “a more flexible object-oriented approach”? Maybe I’m being pedantic here, but that just makes no sense to me at all.
(It appears that the most widely used prototype-based OO language is Javascript. I am aware that its version of OO is often regarded as a hack, but it *is* still more flexible than OO in, say, Java. (And probably Python and Ruby as well, although I haven’t played enough with Javascript to make a bold claim like that. :-) For an example of what a real prototype-based system can do, see Io.)
Of course it’s possible that I misunderstand this, or that AS1′s version of prototype-based OO was so crippled that the class-based successor actually was an improvement over the old situation. I am currently looking into how AS3 handles all this. As far as I can tell right now, there is still some prototype-like stuff left. ^_^
Michael Foord said,
March 30, 2008 @ 9:46 am
“Prototype-based OO is, by its very nature, more flexible than class-based.”
Hmmm… I’d be interested to hear the justification for that statement.
John Cowan said,
March 30, 2008 @ 10:15 am
Flexibility is in the mind of the programmer. C pointers are more flexible than arrays, but a language with both arrays and pointers feels more flexible, easier to adapt to one’s currently preferred way of thinking, even when (as in C) arrays are little more than syntactic sugar over pointers.
Hans Nowak said,
March 30, 2008 @ 12:34 pm
Ah! Both of my readers speak up! :-) I shall prepare a post in which I explain my position.
creidiki said,
April 3, 2008 @ 6:04 am
Hey, there’s at least 3 of us! :D
dan said,
July 20, 2008 @ 3:27 am
It seems to me that the addition of ‘classes’ to javascript is a sensless hack aimed at making the language more accessable to class-based oo minded people who can’t get their head around the prototype paradigm.
JS doesn’t need it and it’s inclusion in JS2 bothers me.