One, foreword
The “use strict” directive is new in JavaScript 1.8.5 (ECMAScript5). It is not a statement, but is a literal expression that was ignored in older versions of JavaScript. The purpose of “use strict” is to specify that code should be executed under strict conditions, in which you cannot use undeclared variables.
Second, strict schema declaration
Add “use strict” to the javascript script or function header; Expression to declare.
Third, strict mode restrictions
- Undeclared variables are not allowed
- Objects or variables cannot be deleted
- Deleting functions is not allowed
- Duplicate names of variables are not allowed
- Escape characters are not allowed
- Assignment to read-only attributes is not allowed
- Disallow the this keyword to refer to global objects (inside functions or in Es6 classes (strict mode is turned on by default) where this refers to ‘undefined’).
function f() {
"use strict"
this.a = 1 // Error: this is undefined
}
Copy the code
Next: How to solve the This pointing problem in class components.