HTML and CSS Reference
In-Depth Information
"use strict";
// Local scope is evaluated as strict code
Strict mode can be enabled for evaled code as well, either by making a direct
call to eval from within other strict code, or if the code to be evaled itself begins
with the strict directive. The same rules apply to a string of code passed to the
Function constructor.
8.3.2 Strict Mode Changes
The following are changes to the language in strict mode. No Implicit Globals
Implicit globals is likely JavaScript's least useful and certainly least appreciated
feature. In ES3, assigning to an undeclared variable does not result in an error, or
even a warning. Rather, it creates a property of the global object, paving the way for
some truly obscure bugs. In strict mode, assigning to undeclared variables results
in a ReferenceError . Listing 8.18 shows an example.
Listing 8.18 Implicit globals
function sum(numbers) {
"use strict";
var sum = 0;
for (i = 0; i < numbers.length; i++) {
sum += numbers[i];
return sum;
// ES3: Property i is created on global object
// ES5 strict mode: ReferenceError Functions
Strict mode offers some help when dealing with functions. For instance, an error will
now be thrown if two formal function parameters use the same identifier. In ES3
implementations, using the same identifier for more than one formal parameter
Search WWH ::

Custom Search