The JavaScript Version

The JavaScript version allows the game to be played online, and acts as a demo of the HTML5 canvas element.

The translation uses a few of the object oriented techniques of the original Java version, but without being too strict about it.

Following Douglas Crockford's notes, classes with private fields are implemented using this style:

function Constructor()
{
  var self = this;
  var privateField1;
  var privateField2;

  this.publicMethod1 = method1;
  this.publicMethod2 = method2;

  function method1(...) { ... }
  function method2(...) { ... }

  function privateMethod3(...) { ... }
  function privateMethod4(...) { ... }
}

This is quite helpful in matching the structure of the original Java code, but has some disadvantages. One is that the keyword this can't be used properly, because of a 'bug' in JavaScript, hence the self field. Another is that conventional JavaScript debugging can't be used, because the private fields aren't visible to the debugger. A final one is that the scheme doesn't mix well with JavaScript's prototype-based inheritance mechanism. The final one is that the technique uses more memory - the private fields of an object are held in a separate closure object. Despite these potential disadvantages, this approach does help with robust development.


Back