First, Objective 1, eliminate some unreasonable JavaScript grammar, not rigorous places, reduce some strange behavior; 2, to eliminate the code to run some unsafe, to ensure the security of code operation, 3, improve the efficiency of the compiler, increase the speed of operation; 4. Pave the path for new versions of JavaScript in the future Second, the use of signs
"Use strict"; (Note: The old version of the browser will ignore it as a line of ordinary strings)
Third, how to call 1, for the entire script file: placed in the first line of the script file. 2. For a single function: Put in the first line of the function body. 3, the script file of the workaround: Put the entire script file in an immediate execution of the anonymous function. Iv. syntax and behavior changes 1, global variables explicitly declared: variables must first be declared with Var, then used, 2, static binding: Which object the property and method belong to, determined at the compile stage. This facilitates compilation efficiency and makes the code easier to read and less unexpected. (1) Prohibit the use of with statements: because the WITH statement cannot determine at compile time which object the property belongs to, (2) Create an eval scope: no longer be able to generate global variables, the variables generated can only be used within the eval;
"use strict" ; var x = 1; console.info(eval( "var x = 5; x" )); // 5 console.log(x); // 2 |
3. Enhanced security measures:
(1) Prohibit the This keyword from pointing to global objects:
function f() { "use strict" ; this .a = 1; } f(); // 报错,this未定义 var f1 = new f(); console.log(f1.a); |
(2) Prohibit traversal of the call stack inside a function:
function f() { "use strict" ; f1.caller; // 报错 f1.arguments; // 报错 } |
4. Disable the deletion of variables: Only the object property of configurable set to True can be deleted;
"use strict" ; var x; delete x; // 语法错误 var o = Object.create( null , { ‘x‘ : { value: 1, configurable: true }}); delete o.x; // 删除成功 |
5. Explicit error
(1) Normal mode, the read-only property of an object is assigned, will not error, will only silently fail. Strict mode, will be an error.
"use strict" ; var o = {}; Object.defineProperty(o, "v" , { value: 1, writable: false }); o.v = 2; // 报错 |
(2) In strict mode, an error is given when an attribute that is read by a getter method is assigned.
"use strict" ; var o = { get v() { return 1; } }; o.v = 2; // 报错 |
(3) In strict mode, adding new attributes to prohibited objects will cause an error.
"use strict" ; var o = {}; Object.preventExtensions(o); o.v = 1; // 报错 |
(4) Strict mode, delete a non-deleted property, will error.
"use strict" ; delete Object.prototype; // 报错 |
6, Duplicate name error: (1) The object cannot have the name of the attribute: Normal mode of the last assignment will overwrite the previous value, strict mode is a syntax error, (2) The function can not have the name of the parameter, 7, prohibit octal notation: Normal mode, the first digit of the integer if 0, indicating that this is an octal number, such equals 10 binary of 64. This notation is prohibited in strict mode and will be an error. 8, the arguments object limit (1) does not allow the assignment of arguments, (2) arguments no longer track changes in parameters, (3) prohibit the use of Arguments.callee: This means that it cannot be called within the anonymous function itself. 9. The function must be life in the top layer: Strict mode only allows functions to be declared at the top level of the global scope or function scope. That is, it is not allowed to declare a function within a block of code that is not a function.
"use strict" ; if ( true ) { function f() { } // 语法错误 } for ( var i = 0; i < 5; i++) { function f2() { } // 语法错误 } |
10, reserved word Strict mode added some reserved words: Implements, interface, let, package, private, protected, public, static, yield. Use these words as variable names will be error;
Strict pattern recognition