Live data from Hacker News

Prototype based vs. class based inheritance

stackoverflow.com

1–10 of 26 posts

Re: Prototype based vs. class based inheritance

#2
See: http://www.c2.com/cgi/wiki?ClassesPrototypesComparison

In my experience, prototype-based OOP is less prone to the typical OOP over-analysis - if you just need one object that does something, you just assemble it, that's it, problem solved. Since the default stance isn't designing a generic class for all possible subtypes that exhibit related behavior and blah blah blah, there's less push toward overthinking things.

Prototype-based OOP is also well-suited to runtime modification, since the underlying implementation is usually simpler, but that's a distant second to the above benefit. And while prototypes are sometimes assumed to be less efficient than classes, look at the research from Self* - While some popular languages have poor implementations, it's not inherently less efficient.

Also, I suspect prototypes integrate more smoothly with non-OOP code than classes, based on experience with Lua (which isn't strictly OOP, but typically uses prototypes). To some extent, this blurs with dynamic typing, though - there's only one statically typed + prototype-based OOP language I know of, Günther Blaschek's Omega (described in _Object-Oriented Programming With Prototypes_).

* http://selflanguage.org/documentation/published/index.html Some of the people involved later worked on the "JVM". Perhaps you've heard of it?

Re: Prototype based vs. class based inheritance

#4
Some thoughts:

Given a sufficiently flexible and late-bound class system (like Smalltalk's), the differences between prototypes and classes can blur.

But in general I would say that it is easier to build classes on top of prototype systems than vice versa, which is one argument for prototypes.

All prototype implementations aren't the same, though, any more than Smalltalk and C++ both have the same class systems! Javascript and Self are quite different in some ways, especially when it comes to delegation (ie inheritance) and iolanguage and research such as Kevo are different again.

Despite the first answer to the linked question, I don't think that either class based or prototype based languages are easier to write a VM for.

What attracts me most is that prototype systems are conceptually simpler in an Occam's Razor sort of way. Instead of needing two concepts: classes and instances, we only need one: objects.

Re: Prototype based vs. class based inheritance

#5

Some thoughts: Given a sufficiently flexible and late-bound class system (like Smalltalk's), the differences between prototypes and classes can blur. But in general I would say that it is easier to build classes on top of prototype systems than vice versa, which is one argument for prototypes. All prototype implementations aren't the same, though, any more than Smalltalk and C++ both have the same class systems! Java…

> What attracts me most is that prototype systems are conceptually simpler in an Occam's Razor sort of way.

That's true, but I find you end up having to make the difference. Yes, prototype inheritance is conceptually simpler but in practice it's more complex. In JavaScript, for example, OO code tends to be harder to follow and includes more boilerplate code. You have to make up for the simplicity of the platform in order to get your work done. In more traditional OO languages, inheritance might be more limited but it's also more straight forward and easier to implement, calling parent class methods is build in, and the "this" reference works consistently.

Re: Prototype based vs. class based inheritance

#6
post #5

Some thoughts: Given a sufficiently flexible and late-bound class system (like Smalltalk's), the differences between prototypes and classes can blur. But in general I would say that it is easier to build classes on top of prototype systems than vice versa, which is one argument for prototypes. All prototype implementations aren't the same, though, any more than Smalltalk and C++ both have the same class systems! Java…

> What attracts me most is that prototype systems are conceptually simpler in an Occam's Razor sort of way. That's true, but I find you end up having to make the difference. Yes, prototype inheritance is conceptually simpler but in practice it's more complex. In JavaScript, for example, OO code tends to be harder to follow and includes more boilerplate code. You have to make up for the simplicity of the platform in o…

This may be true but if it is I think it must be specific to JavaScript. It's the opposite to my experience with Self, which is that prototypes help my code be clearer and simpler.

Re: Prototype based vs. class based inheritance

#7
post #5

Some thoughts: Given a sufficiently flexible and late-bound class system (like Smalltalk's), the differences between prototypes and classes can blur. But in general I would say that it is easier to build classes on top of prototype systems than vice versa, which is one argument for prototypes. All prototype implementations aren't the same, though, any more than Smalltalk and C++ both have the same class systems! Java…

> What attracts me most is that prototype systems are conceptually simpler in an Occam's Razor sort of way. That's true, but I find you end up having to make the difference. Yes, prototype inheritance is conceptually simpler but in practice it's more complex. In JavaScript, for example, OO code tends to be harder to follow and includes more boilerplate code. You have to make up for the simplicity of the platform in o…

JavaScript has the worst form of prototype inheritance I've ever seen. All the boilerplate code is not the fault of prototypes, but of JS. Also JS prototypes are insanely limited. You can't have multiple delegates and you can't change delegates at runtime. JavaScript's inheritance actually smells like some broken state between class based inheritance and prototypes. It has none of the advantages of either and combines the disadvantages of both. I'll make a longer blog post explaining how prototypes are supposed to work and what their real advantages are. You'll see that JS inheritance is a huuuge design failure and that nobody should ever mention prototypes and JavaScript in the same sentence.

Re: Prototype based vs. class based inheritance

#8
post #5

Earlier quoted context omitted.

> What attracts me most is that prototype systems are conceptually simpler in an Occam's Razor sort of way. That's true, but I find you end up having to make the difference. Yes, prototype inheritance is conceptually simpler but in practice it's more complex. In JavaScript, for example, OO code tends to be harder to follow and includes more boilerplate code. You have to make up for the simplicity of the platform in o…

JavaScript has the worst form of prototype inheritance I've ever seen. All the boilerplate code is not the fault of prototypes, but of JS. Also JS prototypes are insanely limited. You can't have multiple delegates and you can't change delegates at runtime. JavaScript's inheritance actually smells like some broken state between class based inheritance and prototypes. It has none of the advantages of either and combine…

I've studied other prototype languages (Io for example), but JavaScript is the only one that I use on a regular basis or in a professional capacity. I imagine for the vast majority of developers, JavaScript is their only introduction to prototype inheritance.

Re: Prototype based vs. class based inheritance

#9
post #8

Earlier quoted context omitted.

JavaScript has the worst form of prototype inheritance I've ever seen. All the boilerplate code is not the fault of prototypes, but of JS. Also JS prototypes are insanely limited. You can't have multiple delegates and you can't change delegates at runtime. JavaScript's inheritance actually smells like some broken state between class based inheritance and prototypes. It has none of the advantages of either and combine…

I've studied other prototype languages (Io for example), but JavaScript is the only one that I use on a regular basis or in a professional capacity. I imagine for the vast majority of developers, JavaScript is their only introduction to prototype inheritance.

Yeah, and that's a real shame because those developers will get a really bad impression of prototypes (unless they only use one of the class emulation libs and never learn about prototypes, of course).

Re: Prototype based vs. class based inheritance

#10

See: http://www.c2.com/cgi/wiki?ClassesPrototypesComparison In my experience, prototype-based OOP is less prone to the typical OOP over-analysis - if you just need one object that does something, you just assemble it, that's it, problem solved . Since the default stance isn't designing a generic class for all possible subtypes that exhibit related behavior and blah blah blah, there's less push toward overthinking thi…

See also Lisaac (http://en.wikipedia.org/wiki/Lisaac), another prototype-based OO language that's statically typed.
Post reply on HN