I. Introduction of strict mode

In addition to normal mode, ECMAscript 5 adds a second mode: “Strict Mode”. As the name suggests, this mode makes Javascript run under more stringent conditions.

The main objectives of the Strict Mode are as follows:

  • Eliminate some unreasonable and inaccurate Javascript syntax, reduce some weird behavior;

  • Eliminate some unsafe code operation, to ensure the safety of code operation;

  • Improve the efficiency of the compiler, increase the running speed;

  • Set the stage for future versions of Javascript.

“Strict mode” represents a more sensible, secure and rigorous way of developing Javascript. It is now supported by major browsers, including IE 10, and many major projects have begun to embrace it.

On the other hand, the same code in “strict mode” may run differently; Some statements that work in “normal mode” will not work in “strict mode”.

Two, strict mode use

1. Turn on strict mode for the entire script

To enable strict mode for the entire script file, place a specific statement “use strict” before all statements; (or ‘use strict’;)

// Strict mode syntax is enabled throughout the script"use strict";
var v = "Hi! I'm a strict mode script!";
Copy the code

2. Turn on strict mode for functions

To turn a function on strict mode, make “use strict”; (or ‘use strict’;) The declaration precedes all statements in the function body verbatim.

function strict(){// function level strict schema syntax'use strict';
  function nested() { return "And so am I!"; }
  return "Hi! I'm a strict mode function! " + nested();
}
function notStrict() { return "I'm not strict."; }
Copy the code

Three, the change of strict mode

1. Common variables

  • In strict mode, variables must be declared with the var command before they are used.
  • An error is reported when assigning a value to an unwritable attribute in strict mode.
  • Assigning a read-only attribute in strict mode will cause an error.
  • An error is reported when adding a new attribute to an object that has no extension in strict mode.
  • An error is reported when an undeletable attribute is deleted in strict mode.
  • Deleting declared variables in strict mode will cause an error.
  • For octal syntax in strict mode :var n = 023 and var s = “\047” will return an error.
// In strict mode, variables must be declared with the var command before they are used. // In normal mode, variables are assigned without declaration and are global by default. demo1 ="success"; console.log(demo1); // An error is reported when assigning an unwritable attribute in strict mode. var demo2 = {}; Object.defineProperty(demo2,"x", { value: 42, writable: false}); demo2.x = 9; // Assigning read-only attributes in strict mode will result in an error. var demo3 = { getx() { return17. }}; demo3.x = 5; // In strict mode, an error will be reported when adding a new attribute to an object whose extension is prohibited. var demo4 = {}; Object.preventExtensions(demo4); demo4.newProp ="ohai"; // An error is reported when an attribute cannot be deleted in strict mode. delete Object.prototype; // Failed to delete variables in strict mode. Only the 64x is configuredtrueObject property can be deleted. var x; delete x; Var demo5 = object. create(null, {'x': {
    value: 1,
    configurable: true}}); delete demo5.x; Var n = 023 and var s ="\ 047"Will be an error. var n = 023; / / an errorCopy the code

2, the same name problem

In strict mode, functions cannot have arguments with the same name

// Arguments [I] can be used as arguments.function demo6(a,a,b){return; } / / an errorCopy the code

3. Disallow the this keyword from pointing to global objects

In strict mode, this in globally-scoped functions no longer refers to the world but to undefined. If you forget to add new when using the constructor, this will no longer refer to the global object, but will instead refer to undefined.

function demo7_1(){
    console.log(this);
}
function demo7_2() {function demo7_3(){
    console.log(this);
    }
    demo7_3();
}
demo7_1(); //undefined
demo7_2(); //undefined

function demo7_4(){   this.a = 1; }; demo7_4(); // Error: if you forget to add new to the constructor, this no longer refers to the global object, but undefined.Copy the code

4. Static binding

  • Disallow the with statement
  • The eval statement is itself a scope, and the variables it generates can only be used inside eval.
// Disable with var demo8 = 1; With (o){// error     demo8 = 2; } // In normal mode,evalThe scope of a statement depends on whether it is in the global or function scope. // In strict mode,evalThe statement itself is a scope and can no longer generate global variables. The variables it generates can only be used forevalInternal. // In strict mode,evalThe string passed in the statement is also executed in strict mode.function demo9() {
    var x = 2;
    eval("var y = 1; console.log(y); "); / / 1eval("var x = 12"); console.log(x); //2 console.log(y); Y is not defined} demo9();Copy the code

Arguments object limits

  • Arguments assignment is not allowed
  • Arguments no longer tracks parameter changes
  • Arguments.callee is disallowed
Arguments ++ is not allowed; // In non-strict mode, changing the value of an index attribute in the arguments object changes the value of the parameter corresponding to that attribute, and vice versa. // In strict mode arguments objects are created and initialized as copies of parameters, so changes to arguments objects do not affect the parameters.function demo10_1(a) {
    a = 2;
    return[a, arguments[0]]; } console.log(demo10_1(1)); // normal mode is [2,2]function demo10_2(a) {
    "use strict"
    a = 2;
    return[a, arguments[0]]; } console.log(demo10_2(1)); // strict mode is [2,1] // arguments.callee var demo11 =function() { returnarguments.callee; }; demo11(); / / an errorCopy the code

6. Disallow traversing the call stack inside a function

function demo12(){ demo12.caller; / / error demo12. The arguments; } demo12();Copy the code

7. Reserved words

Using future reserved words (which may be used in ECMAScript 6):implements, interface, let, package, private, protected, public, static, and yield as variable or function names raises an error.