ECMAScript 5 was standardized in late 2009 but only recently has it has started showing up in browsers. It supersedes the 3rd edition, which was ratified in 1999. ECMAScript 5 is actually two languages, ES5/Default and ES5/Strict. Future versions are going to be built on top of ES5/Strict and it is recommended that the default version be avoided.
With ECMAScript 5 a lot of effort was put into ensuring the standard matched reality. In cases where the majority of the browsers are in conflict with the standard the standard itself was changed.
- Don’t break the web
- Improve the language for the users of the language
- Third party security (mashups)
- No new syntax
- And a non-goal was “protect stupid people from themselves”
Not all of these goals were met, but they got close. The biggest problem is the introduction some new syntax that, if used, will cause problems in older browsers. These syntax changes include:
- Trailing commas in object literals no longer count as an extra item in the resulting array
- Reversed words may be used as property identifiers
- Property Syntax: Getters and setters are now supported in what are known as “accessor properties”
- Multiline string literals: Note that this syntax is somewhat clumsy, a trailing space will result in a syntax error
Properties in ECMAScript 5
In ECMAScript 5 there are two types of properties, data and accessor. A data property is analogous to a field in other OOP languages while accessor properties are like .NET properties. In addition to a value, there are several traits that each property has.
- writable: If set to false the property’s value can no longer be modified. Once set to false it cannot be changed back to true.
- enumerable: If set to false the property can no longer be enumerated in for-in loops. Once set to false it cannot be changed back to true.
- configurable: If set to false the property itself cannot be altered or deleted. Once set to false it cannot be changed back to true.
- get/set: These are the getter and setter functions needed for accessor properties
To make setting these traits across a whole object easier you can call preventExtensions, seal, or freeze to lock down the object.
The bulk of the new functionality came in terms of functions. A gentlemen who goes by the handle kangax has posted a ECMAScript 5 compatibility table listing the functions and where we stand in terms of compliance across the browsers. Some of the highlights include:
- Methods for examining and controlling an object’s meta-data including making an object immutable and non-extensible
- Methods for accessing arrays in a functional-style including map, filter, reduce, and forEarch
- Standardized representation for dates as strings. The chosen format is simplification of ISO 8601.
- Functions for converting objects to and from JSON. These are expected to offer better performance than the library versions currently in use.
Strict mode is primarily designed to prevent many of the problems caused by bad design decisions in the past. Much like Option Explicit in Visual Basic, developers are encouraged to use strict mode whenever possible. Switching to strict mode requires the statement shown below to be placed at the start of a file or function.
- New reserved words: implements, interface, let, package, private, protected, public, static, and yield. These aren’t in use yet but may find their way into ECMAScript 6.
- No more implied global variables in functions
- The implied ‘this’ is no longer bound to the global object. This means it is necessary to write “window.addEventListener” instead of just “addEventListener”.
- The operations “apply” and “all” do not default to the global object
- No “with” statement. The argument is that this causes unnecessary performance problems and cannot be used safely. However the removal of implied global variables eliminates the main objections to its existance.
- Setting a writeable:false property will throw an exception instead of silently failing
- Deleting a configurable:false property will throw an exception instead of silently failing
- eval is run in a restricted scope
- arguments are not linked to parameters. This will allow for performance improvements in the future.
- No more arguments.caller or arguments.callee
- No more octal literals. They are simply not needed.
- Duplicate names in an object literal or function parameters are a syntax error
- Forgetting to use the new prefix will throw an exception, not clobber the global object.
It is highly recommended that JSLint be used to statically check for strict mode violations. This won’t catch everything but it will put you in a much better position.
Technology and Political Concerns
JSON is frozen, so adding a date literal that it can understand will require a replacement format.
Internet Explorer 6 is still far too important. ECMAScript 5 is largely compatible with IE 6, but the changes they want for ECMAScript 6 won’t be. So if IE 6 support is still a concern two years from now ECMAScript 6 will simply be dead on arrival.
Nothing is certain, but in about two years we could be seeing a lighter, more expressive syntax, more expressive literals, tail recursion, modules and namespaces, and proxies for missing properties. This is just the tip of the iceberg and they are concerned about allowing too much to be added at once. This is what caused the failure and abandonment of the ECMAScript 4 standard.
Note: Most of the information in this article was based on a presentation given by Doug Crockford at MIX 2011 titled ECMAScript 5: The New Parts.
Mike Hartington Jul 26, 2015