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.
The new features included in ECMAScript 5 are as follows:
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';
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'ECMAScript 5 includes the following syntactic changes:
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'ECMAScript 5 brought several additions to JavaScript’s standard library. This section lists them by category.
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()
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()
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()
The following resources will be useful if you need to work with legacy browsers: