JavaScript strict mode basis 05--

Strict mode:

In addition to the normal operating mode, ECMAscript5 added a second mode of operation: "strict mode" (strict mode). As the name suggests, this model is the Javascript running under more stringent conditions.

 Strict mode of action:

 1. Eliminate the unreasonable JS grammar, imprecise place, reducing some of the bizarre behavior;
    2. remove some of the insecurity code running, to ensure the safe operation of the code;
    3. To improve compiler efficiency, increase the operating speed;
    4. pave the way for future versions of the new JS
 
    JS strict mode reflects the more reasonable, safer and more rigorous development, including IE10, including major browsers already support it
 Note: The same code in "strict mode", there may be a different operating results, some of the "normal mode" to the normal operation of the statement, under "strict mode" will not run

Strict pattern of usage:

    How to enter strict mode? 
    Mark to strict mode, writing this line statement "use strict" 
    old browsers take him for a bunch of ordinary strings, ignore it 

    , there are two ways to call "strict mode", suitable for different occasions; for the entire script file : the
     "use strict" on the first line of the script file, the whole script file 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. For a single function : the

    "use strict" on the first line of the function, the entire function to "strict mode". Work written script files : because the first call is not good for file merging, so the better approach is to borrow the second method, the entire script files in a anonymous function immediately executed 

    

    anonymous function: no need to call, direct execution may be invoked from 
    (function () { 
        the console.log (. 1); 
    }) ()

After entering the strict mode behavior changes:

1. When a global variable declaration, must be added the keyword ( var )
        Normal mode: A = 10; the console.log (A)     // 10 
        strict mode: A = 10; the console.log (A)     // A IS Not defined
2.this not the global object
        Normal mode: function Fn () {the console.log ( the this )}         // window 
        strict mode: function Fn () {the console.log ( the this )}         // undefined       
The same name is not allowed within the parameters 3. Functions
        Normal mode: function Fn (A, B, B) the console.log {(A, B)} 
                Fn ( l, 2,3)         // l, 3 
        strict mode: function Fn (A, B, B) {}
          / / error: duplicate parameter name not allowed in this context does not allow duplicate parameter name in this context
4.arguments objects
        4.1 arguments object (argument) is not allowed to dynamically change 
            the normal mode: function Fn (A) { 
                        A = 20 is ; 
                        the console.log (A);                 // 20 is 
                        the console.log (arguments [0]);      // 20 is 
                    } 
                    Fn ( 10 ); 

            strict mode: function Fn (A) { 
                        A = 20 is ; 
                        the console.log (A);                 // 20 is 
                        the console.log (arguments [0]);      // 10
                    } 
                    Fn ( 10 );
         4.2 arguments from the object not allowed to be invoked (recursively) 
            Normal mode: function Fn (A) {
                         IF (A ==. 1 ) {
                             return . 1 ; 
                        } 
                        return The arguments.callee (. 1-A) + A; 
                    } 
                    Fn ( . 3);             // . 6 
            strict mode: function Fn (A) {
                         IF (A ==. 1 ) {
                             return . 1 ;
                        } 
                        Return The arguments.callee (. 1-A) + A; 
                    } 
                    // error: 'Caller', 'the callee', and 'arguments' Properties On May Not BE Functions accessed MODE ON or strict Objects The arguments for Calls to Them 
                    // given : "caller", "arguments" , "callee", can not be used in strict mode

    

Guess you like

Origin www.cnblogs.com/wuziqiang/p/12005527.html