Prototype based object models

The Mozilla Developer Center has a great guide for Javascript. This guide provides one of the best explanations I’ve read on the Javascript object model and on prototype based object models in general.

Although I have only programed with languages that provide class based object models I believe that that prototype based object models are very easy to understand if you know the primitives of object oriented programming.

Prototype based object models are an interesting idea that suits very well with dynamic languages like Javascript. The only thing that I don’t really like is the syntax. For example creating a new “class” that inherits from an Employee “class” looks like this in Javascript:

function Manager () {
  this.reports = [];
Manager.prototype = new Employee;

And in Java:

public class Manager extends Employee {
  public Employee[] reports;
  public Manager () {
    this.reports = new Employee[0];

I prefer the Java version because the whole definition is included in one syntactic unit (a class definition), instead two in Javascript (a function definition and an assignment).

If I am correct, Ruby (BTW I don’t know Ruby and Javascript) has a class based object model but also has most of the features of languages that are prototype based. For example in Ruby, you can add or modify, at runtime, methods of classes and instances.