5.4.5. Strict Mode¶
5.4.5.1. Introduction¶
In ES5, in addition to the normal run mode, strict mode was added, which makes the code explicitly leave the “sloppy mode/sloppy mode/sloppy mode” us (sloppy) mode under stricter conditions run. Strict mode isn’t just a subset: it’s produced to form semantics that differ from normal code.
The main purpose of introducing strict mode is to:
Eliminates some of the original silent errors by throwing errors
Eliminate some unreasonable and imprecise aspects of JavaScript syntax and reduce some weird behaviors
Eliminate some insecurities of code operation and ensure the safety of code operation
Fixed some bugs that made it difficult for the JavaScript engine to perform optimizations, improved compiler efficiency,and increased running speed
Disables some syntax that may be defined in future versions of ECMAScript, paving the way for future versions of JavaScript
5.4.5.2. Invocation¶
Strict mode is enabled using the string "use strict";
.For the entire script file,``”use strict”`` can be placed in the first line of the script file to run the entire script in strict mode. If this line of statements is not on the first line, it will not take effect and will run in normal mode.
For a single function, put "use strict"
in the first line of the function body, and the entire function runs in strict mode.
5.4.5.3. Behavior change¶
In strict mode, there are mainly the following behavior changes:
5.4.5.3.1. Explicit declaration of global variables¶
In normal mode, if a variable is assigned a value without a declaration, it defaults to a global variable. Strict mode prohibits this usage, global variables must be declared explicitly.
"use strict";
for(i = 0; i < 2; i++) { // ReferenceError: i is not defined
}
5.4.5.3.2. Disallow the use of the with statement¶
The with statement cannot be determined at compile time, which object the attribute belongs to, which will affect the compilation efficiency, so it is prohibited in strict mode.
5.4.5.3.3. Creating eval scopes¶
In normal mode, the scope of an eval statement depends on whether it is in global scope or function scope. In strict mode, the eval statement itself is a scope and can no longer generate global variables. The variables it generates can only be used inside eval.
5.4.5.3.4. Disabling deletion of variables¶
Variables cannot be deleted in strict mode. Only object properties with configurable set to true can be deleted.
5.4.5.3.5. Explicit error reporting¶
Some errors in normal mode will only fail silently, but in strict mode, errors will be reported, including the following scenarios:
Assign a read-only property to an object
Assign a value to a property read using a getter method
Add new properties to objects that are prohibited from extending
delete an undeletable property
5.4.5.3.6. Syntax errors¶
Strict mode adds some syntax errors, including:
Objects cannot have properties with the same name
Functions cannot have parameters with the same name
Forbid octal notation
Functions must be declared at the top level
- Add reserved words
class
enum
export
extends
import
super
5.4.5.3.7. Security Enhancements¶
Forbid this keyword to point to the global object
It is forbidden to walk the call stack inside a function
5.4.5.3.8. Restricting the arguments object¶
assignment to arguments is not allowed
arguments no longer tracks changes to arguments
disallow use of arguments.callee