Detailed Javascript strict mode (turn)

I. Overview

In addition to the normal operating mode, ECMAscript 5 added a second mode of operation: "strict mode" (the MODE strict). As the name suggests, this mode enables Javascript run under more stringent conditions.

The purpose of the "strict mode", mainly in the following:

  • - eliminate some unreasonable Javascript syntax, not rigorous place, reducing some of the bizarre behavior;
  • - eliminate some of the insecurity code running, to ensure the safe operation of the code;
  • - improve compiler efficiency, increase the operating speed;
  •   - for the future of the new version of Javascript pave the way.

"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.

On the other hand, the same code in "strict mode", there may be a different operating results; some of the "normal mode" to run the statement, under "strict mode" will not run. Master the content, contribute to a more detailed in-depth understanding of Javascript, so that you become a better programmer.

This paper will "strict mode" to do a detailed introduction.

Second, the entry flag

Enter "strict mode" flag, is the following line statement:

"use strict";

The old version of the browser will use it as an ordinary string line, to be ignored.

Third, how to call

"Strict mode" There are two ways of calling methods, suitable for different occasions.

3.1 for the entire script file

The first line will "use strict" on the script file, the entire script will be "strict mode". If this line is not the first line of the statement, would be invalid, the entire script to "normal mode". If the different modes of the code files into one file, it requires special attention.

(Strictly speaking, not produced as long as the foregoing statement of the results of actual operation, "use strict" may not be the first line, such as directly with a semicolon back empty.)

<Script>
    "use strict" ; 
    the console.log ( "This is the strict mode." );
 </ Script> 
<Script> 
    the console.log ( "This is the normal mode." );
 </ Script>

The above codes indicate a page has two sections sequentially Javascript code. Before a script tag is a strict mode, the latter is not.

3.2 for a single function

The "use strict" on the first line of the body of the function, the entire function runs to "strict mode."

function strict () {
    "use strict" ;
     return "What is strict mode." ; 
  } 
function notStrict () {
    return "This is the normal mode." ; 
}

3.3 flexible wording of the script file

Since the first call method is not conducive to merge files, so a better approach is to borrow the second method, the entire script file in the anonymous function in an immediate execution.

(function (){
    "use strict";
    // some code here
})();

Fourth, the syntax and behavior change

Strict mode Javascript syntax and behavior, we have made some changes.

4.1 Global variables explicitly declared

In the normal mode, if a variable is not declared value, the default is a global variable. Strictly prohibit such usage patterns, global variables must be explicitly declared.

    "use strict" ; 
     V =. 1; // error, v is not declared 
     for (I = 0; I <2; I ++) { // an error, i undeclared 
     }

Therefore, strict mode, all variables must be declared with the var command before using.

4.2 static binding

Features of a Javascript language, is to allow "dynamic binding," that some of the properties and methods in the end which object belongs to, is not determined at compile time, but at runtime (runtime) determined.

Strict mode for dynamic binding made some restrictions. In some cases, allow only static binding. That is, in the end which object properties and methods of attribution, it is determined at compile time. Doing so will help improve the coding efficiency, but also makes the code easier to read and less accidents.

In particular, to the following aspects.

(1) prohibit the use of the with statement

Because with the statement unable to determine at compile time, which object ownership property in the end.

"use strict" ;
 var V =. 1 ;
 with (O) { // syntax error 
    V = 2 ; 
}

(2) the creation of eval scope

Under normal mode, Javascript language, there are two variable scope (scope): global scope and function scope. The creation of a third strict mode Scope: eval scope.

In normal mode, the scope eval statements, depending on whether it is in the global scope, or in the function scope. In strict mode, eval statement is itself a scope, no longer able to generate a global variable, it generates variables can only be used for internal eval.

"use strict";
var x = 2;
console.info(eval("var x = 5; x")); // 5
console.info(x); // 2

4.3 enhanced security measures

(1) prohibits this keyword refers to the global object

function F () {
    return ! the this ; 
} 
  // returns false, because the "this" global object point, "this!" is to false 
function F () {
     "use strict" ;
    return ! the this ; 
} 
 // returns true, because in strict mode, this value is undefined, so "! this" is true.

Therefore, when using the constructor, if forgot to add new, this is no longer the global object, but given.

function F () {
     "use strict" ;
    the this II.A =. 1 ; 
}; 
F (); // error, this is not defined

(2) prohibits internal function to traverse the call stack

 function F1 () {
     "use strict" ; 
    f1.caller; // error 
    f1.arguments; // error 
  } 
F1 ();

4.4 prohibited delete variables

You can not delete variables strict mode. Only configurable set to true object attributes can be deleted.

   "use strict" ;
  var X;
  Delete X; // syntax error 
  var O = the Object.create ( null , 'X' , { 
      value: . 1 , 
      Configurable: to true 
  }); 
  Delete OX; // successfully deleted

4.5 Explicit error

In normal mode, read-only attribute of an object assignment, does not complain, just silently fail. In strict mode, an error.

 "use strict";
  var o = {};
  Object.defineProperty(o, "v", { value: 1, writable: false });
  o.v = 2; // 报错

In strict mode, to read a property using getter method of assignment, it will complain.

   "use strict";
  var o = {
    get v() { return 1; }
  };
  o.v = 2; // 报错

In strict mode, add new properties to objects ban extended, will complain.

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

In strict mode, delete a property can not be deleted, error.

  "use strict";
  delete Object.prototype; // 报错

4.6 the same name error

Strict mode added some grammatical errors.

(1) can not have the same name attribute objects

In normal mode, if the object has the same name multiple attributes, attributes that last assignment will overwrite the previous value. In strict mode, which is a syntax error.

   "use strict" ;
  var O = { 
    P: . 1 , 
    P: 2 
  }; // syntax error

(2) can not have the same name of function parameters

The normal mode, if the function parameters of a plurality of the same name can be read with arguments [i]. In strict mode, which is a syntax error.

      "use strict" ;
      function F (A, A, B) { // syntax error 
        return ; 
      }

4.7 prohibit octal notation

In normal mode, if the first bit integer 0, indicates that this is an octal number, such as 0100 equal to 64 decimal. Strict Mode prohibit such representation, the first integer is zero, an error.

      "use strict" ;
      var n-= 0100; // syntax error

Limit 4.8 arguments object

arguments are the arguments object functions, and strict mode has limited its use.

(1) does not permit the assignment of arguments

      "use strict" ; 
      arguments ++; // syntax error 
      var obj SET = {P (arguments) {}}; // syntax error 
      the try {} the catch (arguments) {} // syntax error 
      function arguments () {} / / syntax error 
      var F = new new Function ( "arguments", " 'use strict'; return. 17;"); // syntax error

(2) arguments no longer track changes in the parameters

      function F (A) { 
        A = 2 ;
        return [A, arguments [0 ]]; 
      } 
      F ( . 1); // normal mode [2,2 &] 
      function F (A) {
         "use strict" ; 
        A = 2 ;
        return [A, arguments [0 ]]; 
      } 
      F ( . 1); // strict mode is a [2,1]

(3) prohibit the use of arguments.callee

This means that you can not call itself in the internal anonymous function.

      "use strict";
      var f = function() { return arguments.callee; };
      f(); // 报错

4.9 function must be declared at the top level

The future will introduce a new version of Javascript "block-level scope." In order to integrate with the new version, strict mode only allows top-level function declaration in the global scope or function scope. That is not allowed to declare a function in a non-block function.

      "use strict" ;
      IF ( to true ) {
        function F () {} // syntax errors 
      }
      for ( var I = 0; I <. 5; I ++ ) {
        function F2 () {} // syntax error 
      }

4.10 Reserved Words

In order to transition to the new version of Javascript future, strict mode added some reserved words: implements, interface, let, package, private, protected, public, static, yield.

Using these words as variable names will be thrown.

function Package (protected) { // syntax error 
    "use strict" ;
     var the implements; // syntax error 
}

In addition, ECMAscript fifth edition itself provides a number of other reserved words (class, enum, export, extends, import, super), and a major increase in its own browser const reserved word, but also not be used as variable names.

Fifth, reference links

- MDN, Strict mode
- Dr. Axel Rauschmayer,JavaScript's strict mode: a summary
- Douglas Crockford, Strict Mode Is Coming To Town

(Finish)

Reprinted from: http://www.ruanyifeng.com/blog/2013/01/javascript_strict_mode.html

Reproduced in: https: //www.cnblogs.com/JoannaQ/p/3779159.html

Guess you like

Origin blog.csdn.net/weixin_34301307/article/details/93057402