Wednesday, October 31st, 2007

Transitioning from Java Classes to JavaScript Prototypes

Category: Articles, Examples, JavaScript

To class or not to class, that has been a question than many developers have faced as they came from class based OO worlds into the Prototype Oriented world of JavaScript. Much pain has endured for those that try to contort it.

Peter Michaux has detailed transitioning from Java Classes to JavaScript prototypes by looking at the Observer/Observable pattern and showing various implementations in Java and JavaScript ending up with his favourite mixin-able solution:

var observablize;

(function() {

var observable = {

addObserver: function(observer) {
if (!this.observers) {
this.observers = [];

notifyObservers: function() {
for (var i=0; i

Posted by Dion Almaer at 8:40 am
1 Comment

3.2 rating from 22 votes

1 Comment »

Comments feed TrackBack URI

Sadly, the basic premise of the article, “simulating class-based inheritance in JavaScript really doesn’t work” is no longer true. Prototype 1.6 features a rather elegant solution to this problem in the form of the “$super” argument (see for details).

The mixin and observable patterns are still useful, but no longer essential workarounds where true inheritance is required.

Comment by Broofa — November 1, 2007

Leave a comment

You must be logged in to post a comment.