Strict Mode is a new feature of ECMAScript 5. It allows you to place the entire program, or a function, in a "strict" operation context. This strict context prevents certain operations and throws more exceptions.
While ECMAScript 5 is backward compatible with ECMAScript 3, in strict mode, all features that are not favored in ECMAScript 3 are disabled (or thrown an error) rather than compatible.
Enable strict mode has the following benefits:
1. Catch some programming errors and throw exceptions.
2. Prevent some relatively "unsafe" operations (such as accessing global variables) from being performed and exceptions are thrown.
3. Disable some confusing features.
Most information about strict mode can be found on page 223 of the ES5 Code [PDF].
(Note: The strict mode of ECMAScript 5 is different from the strict mode of Firefox)
How to enable strict mode
Add this statement at the beginning of the program to enable strict mode for the entire script:
The code copy is as follows:
'use strict';
You can also enable strict mode only inside the function, so that it will not affect the outside:
The code copy is as follows:
function imStrict() {
'use strict';
// ... your code ...
}
A statement that enables strict mode is just a normal string "use strict", without any new syntax. This means there will be no negative impact on the old browser.
One practical application of enabling strict mode inside a function is to define the entire Javascript class library inside a strict mode function, so that it does not affect external code:
The code copy is as follows:
// Non-strict code...
(function(){
"use strict";
// Define your library strictly...
})();
// Non-strict code...
So, what changes have been made in scripts in strict mode?
Variables and properties
Assigning an undefined variable will fail instead of using this variable as a global variable.
Writing a property with a writable property false, deleting a property with a configurable property with a false, or adding a property with a extensible property with a false, will result in an error (these characteristics are pre-agreed). In the past, these operations did not throw exceptions, but simply failed silently.
Performing delete operations on variables, functions, or function parameters will cause an error.
The code copy is as follows:
var foo = 'test';
function test() { }
delete foo; // Error
delete test; // Error
function test2(arg) {
delete arg; // Error
}
Defining the same property inside an object container will cause the exception to be thrown:
The code copy is as follows:
// Error
{ foo: true, foo: false }
eval
Any use of the name "eval" (the main purpose is to point the eval function to a variable or object's property) is prohibited.
The code copy is as follows:
// All generate errors...
obj.eval = ...
obj.foo = eval;
var eval = ...;
for ( var eval in ... ) {}
function eval(){}
function test(eval){}
function(eval){}
new Function("eval")
In addition, declaring new variables through eval will also be invalid:
The code copy is as follows:
eval("var a = false;");
print( typeof a ); // undefined
function
Rewriting arguments object will cause an error:
The code copy is as follows:
arguments = [...]; // not allowed
Parameters with the same name will cause an error:
The code copy is as follows:
(function(foo, foo) { }) // Error
Access to arguments.caller and arguments.callee throws an exception. Therefore, any anonymous function that needs to be used must be named first, for example:
The code copy is as follows:
setTimeout(function later(){
// do stuff...
setTimeout(later, 1000);
}, 1000 );
The arguments, caller and callee properties of the function no longer exist, and the operation to define them is also prohibited.
The code copy is as follows:
function test() { }
test.caller = 'caller'; // Error
Finally, a long-standing (and very annoying) bug has been solved: when null or undefined is used as the first parameter of the Function.prototype.call or Function.prototype.apply method, this inside the function will point to the global object. And strict mode will prevent its execution and throw exceptions:
The code copy is as follows:
(function(){ ... }).call(null); // Exception
with() { }
The with() { } statement completely hangs in strict mode.