ECMAScript 5: The Long-Awaited Javascript Update

Ten years in the making, Javascript 5 doesn’t break new ground but contains useful enhancements.

I should note, however, that this reviver concept isn’t actually new. Open-source libraries already exist that give you a JSON object with a stringify function and a parse function that supports a reviver. Here’s a page that gives you more information, including a link to the open-source library.

Object Enhancements: Setters and Getters

Edition 5 also includes enhancements to objects themselves, perhaps one of the most controversial enhancements is that of property getters and setters. These are essentially functions that you can call for setting and retrieving object members. But the difference between these functions and regular functions is you use syntax that looks like setting and getting the values themselves.

In other words, instead of calling myObject.SetX(10), you can simply use myObject.X = 10. This code will have the effect of calling the setter function.

The reason this is controversial is that some programmers feel such code can result in bugs. They argue that if one programmer is writing a library of code that includes property getters and setters, another programmer might not be aware that calling myObject.X = 10 is actually calling a function, and the function can do what it pleases, possibly changing the 10 to something else.

The opposing argument, however, is that good programmers should know the library they are using. If programmers are providing a library that includes getters and setters, they should make it clear in the documentation how those functions work. And the programmers using the library should read the documentation and be aware of the design. I tend to be in the latter camp. I feel that programmers making use of good documentation should be able to correctly use the getters and setters.

In the specification, the properties of an object that can be accessed through getters and setters are called named accessor properties. Such properties have a name, as well as accessor functions (a getter and a setter) associated with them.

I mentioned that IE 8 includes some of the new Edition 5 features. If you’re interested in learning more about the named accessor properties and want to try them out, check out this article.

In addition to getters and setters, the Object object itself includes several new functions. Some allow you to manipulate the getters and setters of other objects (by passing the object as a parameter); some let you create sealed and frozen objects. Sealed objects, are objects whose properties cannot be deleted or have their attributes changed. Frozen objects are sealed objects with the further restriction that their property values cannot be changed.

For a nice overview of the new Object features, check out this page.

Function Enhancements

The single biggest enhancement to functions is the ability to bind a function to an object, effectively making it become a member of the object. Then, if the function uses the this keyword, this will refer to the object (not the function itself).

In ECMAScript, functions are treated as objects (with the additional feature that they can be called). When you create a new function, you are creating an object based on the built-in Function object. From this Function object, your new function inherits various members (through what’s called its prototype). Edition 5 includes one new member in the Function prototype that all your functions will inherit: bind. This new member is itself a function that you can call to bind the function to an object.