Akiko: Handsome people are concerned
Making: making
Hobby: Americano More Ice!
Project source: project Github source code! Star to walk again
directory
- directory
- define
- Why use Strict mode
- Use strict Specifies the scope of the mode
define
- The statement formats
"use strict"; (official website) <! - or - >'use strict'
Copy the code
- define
The “Use Strict” feature was added in ES5(ECMAScript 5) to indicate that code enforces strict patterns.
- Declare scope
Generally declared at the beginning of the script, with global scope (all code in the script is executed in strict mode)
PS: Special case, if declared inside a function, it has only local scope (only code inside a function is in strict mode)
x = 3.14; // This will not cause an error.
myFunction();
function myFunction() {
"use strict";
y = 3.14; // This will cause an error
}
Copy the code
Why use Strict mode
- It’s easier to write in this mode
security
theJavaScript
- Unacceptable in this mode -> previously acceptable
Wrong grammar
For example, in normal JavaScript, typing a variable name incorrectly creates a new global variable. In strict mode, this raises an error, making it impossible to accidentally create a global variable.
As another example, in strict mode, any assignment to an unwritable property, getter-only property, non-existent property, non-existent variable, or non-existent object raises an error.
Use strict Specifies the scope of the mode
- Variables: var, delete, let variable keywords
- Object: read-only property, object property repeated declaration
- Function: arguments with the same name, arguments object declaration
- Others: this, eval, keywords…