Strict model advantages:
Eliminate some unreasonable and imprecise aspects of Javascript grammar and reduce some unexpected situations.
Eliminate some unsafe aspects of code operation to ensure the safety of code operation;
Improving the efficiency of compiler and increasing the running speed;
Note that the same code may have different results in "strict mode"; some statements that can be run in "normal mode" will not run in "strict mode".
Restrictions on strict models:
Undeclared variables are not allowed to be used:
"use strict"; x = 3.14; // Error reporting (x undefined) "use strict"; x = {p1:10, p2:20}; // Error reporting (x undefined)
Deleting variables or objects is not allowed.
"use strict"; var x = 3.14; delete x; // Report errors
Deletion of functions is not allowed.
"use strict"; function x(p1, p2) {}; delete x; // Report errors
Variable renaming is not allowed:
"use strict"; function x(p1, p1) {}; // Report errors
Octal system is not allowed:
"use strict"; var x = 010; // Report errors
Escaped characters are not allowed:
"use strict"; var x = \010; // Report errors
It is not allowed to assign values to read-only attributes:
"use strict"; var obj = {}; Object.defineProperty(obj, "x", {value:0, writable:false}); obj.x = 3.14; // Report errors
Assignment of an attribute read using the getter method is not allowed
"use strict"; var obj = {get x() {return 0} }; obj.x = 3.14; // Report errors
It is not allowed to delete an attribute that is not allowed to delete:
"use strict"; delete Object.prototype; // Report errors
Variable names cannot use the "eval" string:
"use strict"; var eval = 3.14; // Report errors
Variable names cannot use the arguments string:
"use strict"; var arguments = 3.14; // Report errors
The following statements are not allowed to be used:
"use strict"; with (Math){x = cos(2)}; // Report errors
For some security reasons, variables created in scope eval() cannot be invoked:
"use strict"; eval ("var x = 2"); alert (x); // Report errors
Prohibit this keyword from pointing to global objects.
function f(){ return !this; } // Return false, because "this" points to the global object, and "! This" is false. function f(){ "use strict"; return !this; } // Returns true, because in strict mode, this value is undefined, so "! This" is true. //Therefore, if you forget to add new when using the constructor, this will no longer point to the global object, but will report an error. function f(){ "use strict"; this.a = 1; }; f();// Error reporting, this undefined
Keyword retention
In order to transition to a new version of Javascript in the future, some reserved keywords have been added to Strict Mode:
implements interface let package private protected public static yield "use strict"; var public = 1500; // Report errors
For more information: Newbie Course