HTML and CSS Reference
In-Depth Information
This does not imply that the DOM API will change with ES5, but it does mean
that new APIs do not need to suffer the inconsistency of the DOM API.
8.3 Strict Mode
ECMAScript 5 allows a unit—a script or a function—to operate in a strict mode
syntax. This syntax does not allow some of ES3's less stellar features, is less permissive
of potentially bad patterns, throws more errors, and ultimately aspires to reduce
confusion and provide developers with an easier to work with environment.
Because ES5 is supposed to be backwards compatible with ES3, or at least
implementations of it, strict mode is opt-in, an elegant way to deprecate features
scheduled for removal in future updates.
8.3.1 Enabling Strict Mode
The example in Listing 8.16 shows how strict mode can be enabled by a single string
literal directive.
Listing 8.16 Enable strict mode globally
"use strict";
// Following code is considered strict ES5 code
This simple construct may look a little silly, but it is extremely unlikely to collide
with existing semantics and is completely backwards compatible—it's just a no-op
string literal in ES3. Because it may not be possible to port all ES3 code to strict mode
from the get-go, ES5 offers a way to enable strict mode locally. When placed inside
a function, the directive will enable strict mode inside the function only. Listing 8.17
shows an example of strict and non-strict code side-by-side in the same script.
Listing 8.17 Local strict mode
function haphazardMethod(obj) {
// Function is not evaluated as strict code
with (obj) {
// Not allowed in strict
function es5FriendlyMethod() {
Search WWH ::

Custom Search