New in ECMAScript 5 - JavaScript in Depth - Speaking JavaScript (2014)

Speaking JavaScript (2014)

Part III. JavaScript in Depth

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.

New Features

The new features included in ECMAScript 5 are as follows:

Strict mode (see Strict Mode)

Putting the following line first in a file or a function switches on the so-called strict mode that makes JavaScript a cleaner language by forbidding some features, performing more checks, and throwing more exceptions:

'use strict';

Accessors (see Accessors (Getters and Setters))

Getters and setters allow you to implement the getting and setting of a property via methods. For example, the following object obj contains a getter for the property foo:

> var obj = { get foo() { return 'abc' } };

> obj.foo

'abc'

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 method String.prototype.trim()

§ Access characters via the bracket operator [...]

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):

§ JSON.parse() (see JSON.parse(text, reviver?))

§ JSON.stringify() (see JSON.stringify(value, replacer?, space?))

§ Some built-in objects have special toJSON() methods:

§ Boolean.prototype.toJSON()

§ Number.prototype.toJSON()

§ String.prototype.toJSON()

§ Date.prototype.toJSON()

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.