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!
Prototypes vs. Classes. The most important difference between class- and prototype-based inheritance is that a class defines a type which can be instantiated at runtime, whereas a prototype is itself an object instance.
Most object-oriented languages out there, including Python, are class-based. But JavaScript is instead prototype-based. Over the years, this has led to a lot of confusion, and even more attempts to resolve that confusion, either by faking classes, or by explaining why prototypes are good.
Techopedia Explains Prototype-Based Programming The JavaScript creates a link between the new object and the primary object. The cloned/copied/prototyped object works the same way as the primary object. Java Script, Action Script, Newton Script and MOO are some prototype-based programming languages.
No, it's not prototype based.
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:
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.
The JavaScript guide of MDN has some good points, take a look: https://developer.mozilla.org/en/JavaScript/Guide/Details_of_the_Object_Model
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With