EcmaScript 5

EcmaScript is the specification name for JavaScript. The specification has been through some tumultuous times.

The next version was to be EcmaScript 4. However, the fourth edition was to be as much or more different from the third edition as C++ to C, and the effort ultimately collapsed under it’s own weight. Work now continues under EcmaScript-Harmony. with an interim version 3.1 that itself put on a little weight and became 5. I assume that the number 4 was abandoned to avoid confusion.

I’m going to take fifth edition in three broad sweeps: strict mode, new features, and new methods.

Strict Mode

Starting a file or function with the plain string "use strict"; triggers strict mode. I won’t recount all the details; most are minor or obvious. (See the specification, Annex C.)

Eval can no longer introduce variables into it’s scope, which would can my explode method. At the same time, they’ve banned with (a move even John Resig thinks may have been overkill), Yes, you can manually assign local variables to cut down the length of the reference, but programatic manipulation of the scope appears to be impossible. This isn’t as big a problem as it could be, because strict mode is opt-in, so one could have an un-strict setup section and then a strict function with the remainder, for some purposes at least.

Cleaning up this and the automatic creation of global variables falls under ‘obvious’, but it’s significant enough to at least note.

New Features

The really interesting stuff in the new capabilities. Of course these also suffer from the infamous chicken/egg problem that programs will be more widely useable without the new features for some time to come.

There is a far greater capacity to define, introspect, and modify object properties, including enumerable, writeable, and ‘configurable’. It is also possible to list non-enumerable properties for the first time (a longstanding thorn in the side of Naked Javascript) and there is a standard API for retrieving an object’s prototype.

Once again, I won’t belabor you with the details, already described elsewhere. One particular feature of interest is the ability to define get/set methods, thus creating ‘magic’ properties. This is a critical capability that Canvas P.J.s is missing, but it brings us back to the chicken/egg problem: do I create and maintain a separate version, try to make it multi-version without also making it ugly, or just leave in it’s slightly crippled state?

New Methods

A more tractable difference is a number of new methods. Some provide the aforementioned reflection features and could at best be stubbed out. Others are provided for convenience and efficiency. This part could be a provided by a library, so I went looking for one. The major libraries like jQuery and Prototype are overkill for many of the small projects I tend to work on. I did get a pointer to Underscore, which was previously unknown to me, though still more than I wanted.

So I shut up and made an EcmaScript 5 compatibility library. It’s still pretty untested and I want to make some of the functionality optional; specifically prototype additions and ‘stub’ methods which could mask the fact that the feature is unsupported. I haven’t done anything for the JSON object since I’ve never needed it, but I just saw that John Resig recommends json2.js as providing a compatible API.

Posted Saturday, December 19th, 2009 under Essay.

Comments are closed.