Strict pattern recognition

Source: Internet
Author: User

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";varx = 1;console.info(eval("var x = 5; x"));  // 5console.log(x);  // 2

3. Enhanced security measures:
(1) Prohibit the This keyword from pointing to global objects:

functionf() {    "use strict";    this.a = 1;}f(); // 报错,this未定义var f1 = newf();console.log(f1.a);

(2) Prohibit traversal of the call stack inside a function:

functionf() {    "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";varx;delete x; // 语法错误var o = Object.create(null, {‘x‘: {    value: 1,  configurable: true}});deleteo.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";varo = {};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";varo = {    get v() { return1; }};o.v = 2; // 报错

(3) In strict mode, adding new attributes to prohibited objects will cause an error.

"use strict";varo = {};Object.preventExtensions(o);o.v = 1; // 报错

(4) Strict mode, delete a non-deleted property, will error.

"use strict";deleteObject.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++) {    functionf2() { } // 语法错误}

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.