Chapter 25. New in ECMAScript 5
Table of contents
Buy the book
(Ad, please don’t block.)

Chapter 25. New in ECMAScript 5

This chapter lists features that are available only in ECMAScript 5. Should you have to work with older JavaScript engines, you should avoid these features or enable some of them via a library (how is described later). Note that normally, this book assumes that you are working with modern engines, which fully support ECMAScript 5.

The ECMAScript 5 specification contains the following description of its scope:

The fifth edition of ECMAScript (published as ECMA-262 5th edition)

  • codifies de facto interpretations of the language specification that have become common among browser implementations and
  • adds support for new features that have emerged since the publication of the third edition. Such features include

    • accessor properties,
    • reflective creation and inspection of objects,
    • program control of property attributes,
    • additional array manipulation functions,
    • support for the JSON object encoding format, and
    • a strict mode that provides enhanced error checking and program security.

Syntactic Changes

ECMAScript 5 includes the following syntactic changes:

Reserved words as property keys

You can use reserved words (such as new and function) after the dot operator and as unquoted property keys in object literals:

> var obj = { new: 'abc' };
> obj.new
'abc'
Legal trailing commas
Trailing commas in object literals and array literals are legal.
Multiline string literals
String literals can span multiple lines if you escape the end of the line via a backslash.

New Functionality in the Standard Library

ECMAScript 5 brought several additions to JavaScript’s standard library. This section lists them by category.

Metaprogramming

Getting and setting prototypes (see Getting and Setting the Prototype):

  • Object.create()
  • Object.getPrototypeOf()

Managing property attributes via property descriptors (see Property Descriptors):

  • Object.defineProperty()
  • Object.defineProperties()
  • Object.create()
  • Object.getOwnPropertyDescriptor()

Listing properties (see Iteration and Detection of Properties):

  • Object.keys()
  • Object.getOwnPropertyNames()

Protecting objects (see Protecting Objects):

  • Object.preventExtensions()
  • Object.isExtensible()
  • Object.seal()
  • Object.isSealed()
  • Object.freeze()
  • Object.isFrozen()

New Function method (see Function.prototype.bind(thisValue, arg1?, ..., argN?)):

  • Function.prototype.bind()

New Methods

Strings (see Chapter 12):

New Array methods (see Array Prototype Methods):

  • Array.isArray()
  • Array.prototype.every()
  • Array.prototype.filter()
  • Array.prototype.forEach()
  • Array.prototype.indexOf()
  • Array.prototype.lastIndexOf()
  • Array.prototype.map()
  • Array.prototype.reduce()
  • Array.prototype.some()

New Date methods (see Date Prototype Methods):

  • Date.now()
  • Date.prototype.toISOString()

JSON

Support for JSON (see Chapter 22):

Tips for Working with Legacy Browsers

The following resources will be useful if you need to work with legacy browsers:

  • A compatibility table by Juriy Zaytsev (“kangax”) shows how much of ECMAScript 5 is supported by various versions of various browsers.
  • es5-shim brings most (but not all) of ECMAScript 5’s functionality to browsers that support only ECMAScript 3.
Next: IV. Tips, Tools, and Libraries