ECMAScript 5 was released this week (pdf), generally known as JavaScript™, bringing advances to the basic libraries whilst introducing stricter runtime modes to aid with identifying and removing common coding errors.
Earlier attempts to rationalise ECMAScript 4 largely failed; only Adobe's ActionScript is based on the proposed changes. ECMA didn't even release a version 4 specification owing to different groups being unhappy at the developments; and as such, no browsers support it.
Over the last few years, with large improvements in JavaScript engines like Nitro and TraceMonkey, JavaScript has become performant to the extent that collaborative tools like Google Wave are showing the way forward for on-line applications. There's even Speed Tracer, an extension to Google Chrome, released as part of GWT 2.0, to help optimise the performance of JavaScript applications.
With that in mind, ECMAScript 5 aims to be backwardly compatible with the currently in-use ECMAScript 3 version (to foster quicker adoption across browsers), as well as providing for stricter constraints for developers to avoid common coding pitfalls.
Strict mode
The introduction of strict mode aims to avoid common coding problems in ECMAScript applications. This is achieved with the presence of a lone string literal in a unit (script or function):
"use strict;"
This literal will have no effect on existing runtimes, but new runtimes that target version 5 will turn on strict mode for either the entire script (if at the top of the script) or for a single function (if the first part of a function). This allows for a mixture of strict and non-strict code and for an evolution of existing code. So, what does strict mode actually mean?
- Variables must be declared before use. In other words,
i=3
becomes a runtime error;var i=3
is needed (assumingi
is not in scope at the time) - Eval becomes a reserved word, and introducing new variables through eval cannot occur, so
eval("var i=3"); print(i);
will now throw an error. - Octal literals are no longer used; so
010
is ten, and not eight delete
cannot be used against arguments, functions or variables or other properties with the configurable flag set to falsewith
statements, often a source of errors, are no longer used and considered syntax errors- Functions can no longer have duplicate arguments with the same name
- Objects can no longer have duplicate properties with the same name
- The
arguments
andcaller
variables become immutable - Access to the global object becomes a runtime error
Library extensions
Other extensions are also present in the base library:
- Date now supports the ability to generate ISO8601 formatted dates (such as
20091209T12:34:56Z
) as well as to parse them - String now has a built-in
trim()
method - A new
JSON
object withparse
andstringify
to support efficient generation of JSON data; likeeval
but without the security implications of being able to reduce code. In addition, anyJSONValue
can be used, not justJSONObject
andJSONArray
as specified in RFC 4627. (4627 definesJSON-Text
to be constrained to an object or array.) - A new
bind
builtin has been added, with the same semantics as Prototype's bind() - Array now has standard functions, such as
indexOf()
,map()
,filter()
, andreduce()
- Object now has
seal()
(which prevents new properties being added or existing properties deleted) andfreeze()
(which makes all properties read-only, as well as preventing new properties being added or deleted) Object.keys()
lists all the enumerable properties of the objectObject.getOwnPropertyNames()
lists all the enumerable and non-enumerable propertiesObject.getPrototypeof()
returns the prototype of the given object
Summary
The additions of a standard JSON parsing mechanism and strict mode will be of great benefit to developers, with the potential to translate into smaller libraries for Prototype and other extension libraries required. Parsing ISO dates from a JSON stream now becomes much more portable than before, and looks likely to be the de facto standard for representing dates in the future. Lastly, since this is backwardly compatible and takes cues from existing libraries like Prototype, it is likely that developers and web browsers alike will take to the new features of JavaScript in the near future.