Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The difference between a class-based language (like Java or Python) and a prototype-based language (like Javascript)? [duplicate]

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!

like image 715
Ben Avatar asked Jul 23 '12 21:07

Ben


People also ask

What is the difference between class-based vs prototype-based languages?

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.

Is Python prototype-based or class-based?

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.

Is Java a prototype-based language?

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.

Is Python a prototype-based language?

No, it's not prototype based.


3 Answers

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.
like image 57
jeff Avatar answered Nov 15 '22 19:11

jeff


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.

like image 26
scytale Avatar answered Nov 15 '22 20:11

scytale


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

like image 30
davidbuzatto Avatar answered Nov 15 '22 21:11

davidbuzatto