Strict mode (use strict)

JavaScript strict mode (use strict)

JavaScript strict mode (strict mode) that is run under strict conditions

Use "use strict" directive

"Use strict" directive in ECMAScript5.0 new in

It's not a statement, but it is a literal expression, is ignored in older versions of JavaScript

Supports strict mode browsers:
  Internet Explorer 10 +, Chrome 13+ Firefox 4+, Safari 5.1+, Opera 12+

Back to the top table of contents

Strict Mode Statement

Strict Mode in the head by a script or function to add "use strict"; expression statement.

"use strict"
num = 3;   // 报错 
// Uncaught ReferenceError: num is not defined

Back to the top table of contents

使用"use strict"

  1. Do not allow the use of undefined variables (normal mode will become undefined global variables)
  2. "Use strict" written inside the function, then the internal function is the strict mode
  3. Why use strict mode:
    • Eliminate unreasonable Javascript syntax, not rigorous place, reducing some of the bizarre behavior;
    • Remove some of the insecurity code running, to ensure the safe operation of the code;
    • Improve compiler efficiency, increase the operating speed;
    • Pave the way for future new version of Javascript.

"Strict Mode" reflects Javascript is more reasonable, safer and more rigorous development, including IE 10, including the major browsers, already support it, many large projects have begun to embrace it.

Back to the top table of contents

Strictly limited mode

  1. Not permitted to declare Undefined variable:num = 3
  2. Object is also a need to define variables:x = {a: 1, b: 2}
  3. Allowed to delete variables, objects and functions:delete num || delete x
  4. Not allowed octal, escape:var num = 010; var x = \100
  5. Variables are not allowed the same name:function x(p1, p1) {};
  6. We do not allow read-only attribute assignment:
    "use strict"
    
    var obj = {};
    Object.defineProperty(obj, "x", {value:0, writable:false});
    obj.x = 3; 
    
  7. It is not allowed to use the property getter method of a reading assignment:
    "use strict"
    
    var obj = {get x(){return 0}}
    obj.x = 3; 
    
  8. Permitted to delete a property can not be deleted:delete Object.prototype;
  9. Variable names can not use "eval", "arguments" string keywords such as:var eval = 1, arguments = 1
  10. Due to some security reasons, variable scope eval () call can not be created:eval ("var x = 2");里面的x不能调用
  11. Prohibit this keyword refers to the global object:普通模式的this的值window在严格模式下为undefined
  12. ECMAScript 6 strictly prohibited mode setting properties primitive values. Do not use strict mode, set the property will simply be ignored (no-op), strict mode, will throw a TypeError
    • primitive Value: basic type (basic value basic data types) is neither a method of data objects no. In JavaScript, there are seven basic types: string, number, bigint, boolean, null, undefined, symbol (ECMAScript 2016 new)
    "use strict";
    
    false.true = "";              //TypeError
    (14).sailing = "home";        //TypeError
    "with".you = "far away";      //TypeError
    
  13. With not allowed

Back to the top table of contents

Reserved Keywords

In order to transition to the new version of Javascript future, strict mode added some reserved keywords:

  • implements
  • interface
  • let
  • package
  • private
  • protected
  • public
  • static
  • yield

Back to the top table of contents

Published 43 original articles · won praise 3 · Views 1125

Guess you like

Origin blog.csdn.net/qq_45007419/article/details/104878307