ECMAScript 5 Strict Mode introduction, ecmascriptstrict
Strict Mode is a new feature of ECMAScript 5. It allows you to place the entire program or a function in a "Strict" operating context. This strict context prevents some specific operations and throws more exceptions.
Although ECMAScript 5 is backward compatible with ECMAScript 3, in strict mode, all features that are not supported in ECMAScript 3 are disabled (or throw an error) rather than compatible.
Enabling strict mode has the following benefits:
1. capture some programming errors and throw exceptions.
2. Block operations that are relatively "insecure" (such as accessing global variables) and throw an exception.
3. Disable some confusing features.
Most information about the strict mode can be found on page 223rd of ES5 specification [PDF.
(Note: The strict mode of ECMAScript 5 is different from that of Firefox)
How to enable strict Mode
Add this statement at the beginning of the program to enable the strict mode for the entire script:
Copy codeThe Code is as follows:
'Use strict ';
You can also enable the strict mode only within the function, which does not affect the External:
Copy codeThe Code is as follows:
Function imStrict (){
'Use strict ';
//... Your code...
}
The statement that enables strict mode is just a common string "use strict" without any new syntax. This means that it will not have any negative impact on legacy browsers.
One practical application of enabling the strict mode inside a function is to define the entire Javascript class library inside the function of the strict mode, so that the external code is not affected:
Copy codeThe Code is as follows:
// Non-strict code...
(Function (){
"Use strict ";
// Define your library strictly...
})();
// Non-strict code...
So what is the change in the script in strict mode?
Variables and attributes
The assignment of undefined variables will fail, rather than using this variable as a global variable.
Write an attribute with the writable property of false, delete an attribute with the configurable property of false, or add an attribute with the Extensible property of false, will lead to errors (these features are pre-agreed ). In the past, these operations did not throw an exception, but only failed silently.
Executing the delete operation on variables, functions, or function parameters may cause errors.
Copy codeThe Code is as follows:
Var foo = 'test ';
Function test (){}
Delete foo; // Error
Delete test; // Error
Function test2 (arg ){
Delete arg; // Error
}
Defining the same attributes inside an object container will cause an exception to be thrown:
Copy codeThe Code is as follows:
// Error
{Foo: true, foo: false}
Eval
Any use of the name "eval" (mainly intended to direct the eval function to a variable or an object attribute) is forbidden.
Copy codeThe Code 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:
Copy codeThe Code is as follows:
Eval ("var a = false ;");
Print (typeof a); // undefined
Function
Overwriting the arguments object will cause an error:
Copy codeThe Code is as follows:
Arguments = [...]; // not allowed
Parameters with the same name may cause errors:
Copy codeThe Code 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:
Copy codeThe Code is as follows:
SetTimeout (function later (){
// Do stuff...
SetTimeout (later, 1000 );
},1000 );
The arguments, caller, and callee attributes of the function no longer exist. Defining them is also forbidden.
Copy codeThe Code is as follows:
Function test (){}
Test. caller = 'caller'; // Error
Finally, a long-standing (and annoying) BUG has been fixed: When null or undefined is used as the Function. prototype. call or Function. prototype. when the first parameter of the apply method is used, this inside the function will point to the global object. The strict mode will stop execution and throw an exception:
Copy codeThe Code is as follows:
(Function () {...}). call (null); // Exception
With (){}
The with () {} statement is completely suspended in strict mode.