HTML and CSS Reference
In-Depth Information
8.4.1 Native JSON
ES5 introduces native JSON support, in form of the
JSON
object. It supports two
methods, JSON.stringify and
JSON.parse
to dump and load JSON respectively.
Douglas Crockford's
json2.js
provides a compatible interface for browsers that
does not yet implement the new JSON interface. This means that by loading this
library, we can start using this particular feature today. In fact,
json2.js
has been
widely used for some time, and several browsers already support the native
JSON
object.
Both ES5 and
json2.js
also adds
Date.prototype.toJSON
, which seri-
alizes date objects as JSON by way of
Date.prototype.toISOString
, which
in turn uses a simplification of the ISO 8601 Extended Format. The format is as
follows:
YYYY-MM-DDTHH:mm:ss.sssZ
8.4.2
Function.prototype.bind
The
bind
method, as described in Chapter 6,
Applied Functions and Closures,
is
native to ES5. This should mean improved performance, and less code for libraries
to maintain. The previously provided implementation is mostly equivalent to the
one provided by ES5 apart from a few details. The native
bind
function returns
a native object, which itself has no
prototype
property. Rather than creating a
simple function that wraps the original function, a special type of internal object
is created that maintains the relationship to the bound function such that, e.g., the
instanceof
operator works with the resulting function just like it would with
the bound function.
8.4.3 Array Extras
Lots of new functionality is added to arrays in ES5. Most of these stem from
Mozilla's JavaScript 1.6, which has been around for some time—long enough for,
e.g., Safari's JavaScriptCore to implement them as well. ES5 also adds
Array.
isArray
, which can determine if an object is an array by checking its internal
[[Class]] property. Because
Object.prototype.toString
exposes this prop-
erty, including in ES3, it can be used to provide a conforming implementation, as
seen in Listing 8.21.
Listing 8.21
Implementing
Array.isArray
if (!Array.isArray) {
Array.isArray = (function () {
function isArray(object) {