spiros.blog()
Spiros Tzavellas’s blog, mostly on software development and Java.
Browse: Home / Java, Javascript / Prototype based object models

Prototype based object models

By spiros on December 6, 2005

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.

Posted in Java, Javascript | Tagged oop

« Previous Next »

Pages

  • About
  • Projects

Categories

  • .Net
  • Agile
  • architecture
  • Databases
  • Firefox
  • Java
  • Javascript
  • Linux/Unix
  • Programming
  • Scala
  • Startups
  • Web

Tags

aop architecture aws book C# C/C++ caching circuit-breaker cloud clustering Databases dependency injection design patterns distributed eclipse EclipseLink ejb fp gc guice gwt hibernate Java javaee jhug jpa junit jvm maven mysql oop php podcast ruby Scala scalability scrum seam spring framework struts tools transactions web services wireless xml

Copyright © 2012 spiros.blog().

Powered by WordPress and Hybrid.