Is JavaScript’s new syntactic sugar? Sunday, Jul 12 2009 

JavaScript has a ‘new’ operator for creating objects. Each object in JavaScript has a hidden pointer to a prototype object, from which it inherits properties. This pointer is set when the object is created, and cannot be changed during the lifetime of the object.

When ‘new’ is used, an object is created that has other than the usual default Object prototype object.

From ‘new’ we can define a function create(proto) that returns an object whose prototype object is ‘proto’. (I first saw this in Douglas Cockcroft’s book JavaScript: The Good Parts, but he calls it ‘beget’.)

Conversely, from ‘create’ we can define a function, say ‘New’ that has the same functionality as the ‘new’ operator. (In JavaScript ‘new’ is a keyword, whose meaning can be changed.)

The ‘create’ operator is in some sense simpler than the ‘new’ operator, but each can be defined in terms of the other. According to Wikipedia, syntactic sugar “is syntax designed to make things easier to read or to express, while alternative ways of expressing them exist”.

I think ‘create’ is simpler to use and understand than ‘new’, and so I don’t regard it as syntactic sugar for the creation creation of objects.  The same Wikipedia term also suggests the term  syntactic saccharin for gratuitous syntax which does not actually make programming easier.  I consider ‘new’ to be syntactic saccharin.


Javascript and skeletons Monday, Feb 9 2009 

Here’s an analogy (and I don’t know how useful it is). It’s about writing JavaScript for web pages.

A beetle, for example, has a hard external skeleton, to which everything else is attached. A cat, for example, has a hard internal skeleton, to which everything else is attached. A cat is soft outside (although with teeth and claws) while a beetle is soft inside.

So what’s this got to do with JavaScript and HTML? Well, a simple HTML page spruced up with some JavaScript for say form entry is a bit like a beetle, where the HTML is the hard external skeleton.

But a complex web page, constructed mostly from JavaScript and Ajax supplied data is something else. It’s too big and complex to be supported just by HTML and the DOM. It needs some sort of skeleton. And so perhaps should be built more like a cat.

Implementing super in JavaScript Sunday, Sep 21 2008 

The problem

JavaScript has objects and what is called prototypal inheritance, but it does not have built-in support for many features provided by other languages. In particular, it does not have built-in support for classes and instances. Nor does it have built-in support for methods of one class to call super-class methods.

Here is an example. Class C is a subclass of class B, and both have an init method. We would like the init method of Class C to be able to call the init method of class B. In other words, when writing init for class C, we wish to call the super-class init method. Note that class B might in fact be a subclass of class A, and that B might inherit init from A, and not have an init method of its own.

This post of focussed in the single topic of providing access to super-class. It does not for instance cover how to provide support for class hierarchies and how to manage instance creation.


Over the past few days I’ve been trying to understand John Resig‘s post on Simple JavaScript Inheritance, on both the technical and the practical levels. John’s goal was to extract the soul of the implementations of classical inheritance in base2 and Prototype, and to present them as a stand-alone package.

Well, I’ve not succeeded, but I do understand the problem better. In particular, I couldn’t get a clear understanding of what his _super method did. So to help me understand I set out to write my own. And this is what I came up with.

I hope in a later post to compare the two approaches. Be warned that this post is unavoidable quite technical, and that if you’re a JavaScript novice you’ll learn a lot if you manage to understand it all.


Data objects in JavaScript Saturday, Sep 20 2008 

The Problem

Suppose we want to count the words in a document, or the identifiers in a computer program. We’ll need to keep a dictionary (aka hash or mapping) whose keys are the words and whose values are their frequencies. When we come across a new word (or identifier) we add it to the dictionary as a key, with value equal to one. When we meet a word already in the dictionary, we increase the value by one.

The starting point, of course, is an empty dictionary (or mapping or hash or whatever you want to call it). In JavaScript creating one of these requires some thought, and seems not to have been done before. Here’s the problem. In JavaScript we have to store the data (key-value pairs) in an object, and every object comes with some builtin methods. Here’s some command-line JavaScript:

js> data = {};  // Create empty data object, we hope.
js> === undefined; // true
js> data['foo'] === undefined;  // true
js> data['constructor'] === undefined; // false - not what we want.