History of JavaScript: Part 7

05 Jul 2010 | By Alex Young | Tags language history hoj ecmascript5

This article is part 7 of our History of JavaScript series. Last week I covered the history of JavaScript animation. This week I’ll take a look at ECMAScript 5.

JavaScript 2

If you recall part 3, JavaScript 2 died as the community fought over the direction of the language’s future. ECMA-262 5th edition was approved in 2009. The original press release states:

Work on future ECMAScript editions continues as part of the previously announced ECMAScript Harmony project.

ECMAScript 5th Edition

ECMAScript 5th Edition isn’t as radical as JavaScript 2 was supposed to be. It’s a very different beast. The main changes are relatively conservative:

  • Array methods: indexOf, lastIndexOf, forEach, filter, every, map, some
  • String.trim: remove whitespace from the start and end of a string
  • Native JSON
  • Date: now, toISOString, parse

There are also some more fundamental language changes:

  • Function.prototype.bind
  • Updated object model
  • Strict mode
  • Constants
  • Getters and setters

Updated Object Model

Object gets a lot of new methods: Object.create, Object.getPrototypeOf, Object.keys, Object.getOwnPropertyNames, object descriptors, Object.seal, Object.freeze.

I don’t want to write about all of these new Object methods, but basically: we now have a way of freezing objects, and safely enumerating over methods that want to be enumerated over.

There’s coverage in Enhanced Scripting in IE9: ECMAScript 5 Support and More and Object.getPrototypeOf.

These methods will give more control to API authors and a greater sense of control over objects.

Strict mode

use strict; will instruct the interpreter to run with strict mode. This is a useful way of ensuring that code only uses ECMAScript features, rather than a dialect:

The strict variant of ECMAScript is commonly referred to as the strict mode of the language. Strict mode selection and use of the strict mode syntax and semantics of ECMAScript is explicitly made at the level of individual ECMAScript code units.

Constants

Constants aren’t actually in ECMAScript 5, const is marked as a future reserved word. Some interpreters support it already, although I wouldn’t rely on the behaviour the exhibit being totally future-proof.

Getters and Setters

The keywords get and set are used to decorate functions that are getters or setters:

user = {
  first_name: 'Alex',
  last_name: 'Young',
  
  get full_name() { return this.first_name + ' ' + this.last_name; },
  
  set full_name(name) {
    name = name.split(' ');
    this.first_name = name[0];
    this.last_name = name[1];
  }
};

user.full_name
// returns: Alex Young
user.full_name = 'Alex Smith'
user.full_name
// returns: Alex Smith

You can already use this in Rhino and Firefox, as well as V8 and probably lots of other interpreters.

This is being used out there in the wild, so it’s worth getting used to the syntax.

Future

ECMAScript 5 adds enough syntax sugar to keep developers happy for the foreseeable future. It also addresses some areas in the object model that we were left to code for ourselves. The Array and String changes also add things we’ve previously had to patch ourselves. More importantly, these changes combined with use strict help in areas where JavaScript interpreters had diverged, which means hopefully future browsers (and our code) should be more consistent.

References


blog comments powered by Disqus