The concept of strict mode was introduced from ECMAScript5, with strict mode, you can select within a function for strict global or local error condition detection. First let’s look at the description of strict mode in MDN

  1. Strict mode eliminates some of the original silent errors by throwing errors.
  2. Strict mode fixes some of the bugs that make it difficult for JavaScript engines to perform optimizations: sometimes, the same code can run faster in strict mode than in non-strict mode.
  3. Strict mode disables some syntax that may be defined in future versions of ECMAScript.

How to use strict mode

  1. In general we recommend placing it at the very top of the script.”use strict
    • Engines that support strict mode start this mode,
    • Engines that do not support this pattern ignore this compilation instruction when encountering an unassigned string literal.
  2. You can also use strict mode in functions, as shown in the following example code:
function striceExample(){ 
  "use strict";
  // Other logic......
}
Copy the code
  1. Modern ES6 code, supportclassesmodulesHigh-level language constructs, which are automatically enableduse strict. Therefore, if we use them, there is no need to add the “Use strict” directive.

Difference between strict mode and non-strict mode

In strict mode variables must be declared

In normal mode, if a variable is assigned an undeclared value, it defaults to a global variable. Strict mode prohibits this. In strict mode, if you assign a value to an undeclared variable, the code will raise a ReferenceError at execution time

The with statement is prohibited in strict mode

Because the with statement cannot determine, at compile time, which object a property belongs to, a strict schema can improve compilation efficiency.

with(location){ 
  console.log(href);
}
// Strict mode: throws a syntax error
Copy the code

Create eval scope in strict mode

In normal mode, JS has two kinds of variable scope, global scope and local scope. In normal mode, the scope of an EVAL statement depends on whether it is in the global scope or function scope. In strict mode, the eval statement itself is the scope and cannot generate global variables.

For example:

function testEval(){ 
  eval("var a='lxm'"); 
  alert(a);
}
// Non-strict mode: LXM will pop up when alert(a) is called
// Strict mode: ReferenceError is raised when alert(a) is called
Copy the code

Variables and functions can be declared in eval(), but these variables or functions are valid only in the special scope in which they are evaluated, and are then destroyed.

In strict mode, the this keyword is disallowed from pointing to global objects

In strictly global scoped functions, this is undefined, as in the following code

var name = "lxm";
function showName(){ 
  alert(this.name);
}

showName.call(null);
// Non-strict mode: access global properties
// Strict mode: Throw an error because this is null
Copy the code

In the code above, if in non-strict mode this aims at global objects. The result is a pop-up dialog that says “LXM”.

In strict mode, the function’s this value is null, so an error is thrown when the attribute of null is accessed.

Strict mode disallows traversing the call stack inside a function

Caller: Refers to a reference to a function that calls the current function

function testCaller(){
    "use strict";
    testCaller.caller; / / an error
    testCaller.arguments; / / an error
}
testCaller();
Copy the code

Variables cannot be deleted in strict mode

Only object properties with ConifGurable set to true can be deleted

"use strict"

var x ;

delete x; // Syntax error reported in strict mode

var o = Object.create(null, {'x': {value: 1.configurable: true

}})

delete o.x; // The deletion succeeded
Copy the code

Display error in strict mode

In normal mode, assigning a read-only property to an object does not report an error, but silently fails. An error is reported in strict mode

"use strict";

var obj = {};

Object.defineProperty(obj,"name", {value: 'lxm'.writable: false});

obj.name = 2; // The obj.name attribute cannot be modified. Strict mode will report an error. Normal mode will fail but not report an error
Copy the code

In strict mode, you are not allowed to add new attributes to objects that do not allow extension

"use strict";

var obj = {};

Object.preventExtensions(obj);// Disallow o objects from having extended attributes

obj.name = 1; / / an error
Copy the code

In strict mode, it is forbidden to delete an undeletable attribute

"use strict";
delete Object.prototype; / / an error
Copy the code

Duplicate variable declarations are prohibited in strict mode

let a = 1;
let a = 2; // Error reported in strict mode
Copy the code

Arguments in the function body

In non-strict mode, changes to the values of named arguments are also reflected in the arguments object, for example:

// Change the value of the named parameter
// Non-strict mode: changes are reflected in arguments
function showName(name){ 
  name = "lxm";
  alert(name); //"lxm" 
  alert(arguments[0]); // Non-strict mode: "LXM"
}
showName("lili");
Copy the code

In lattice mode these two values are completely independent.

// Change the value of the named parameter
// Strict mode: changes are not reflected in arguments
function showName(name){ 
"use strict"
  name = "lxm";
  alert(name); //"lxm" 
  alert(arguments[0]); // Non-strict mode: "hi"
}
showName("lili");
Copy the code

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

Arguments with the same name can be returned in strict mode. Arguments [I] can be returned in normal mode

Octal is forbidden in strict mode

In normal cases, the first digit of an integer 0 represents octal. In strict mode, the first digit of an integer 0 is an error

Note: Parsing octal literals using parseInt will be treated as decimal literals starting with 0 in strict mode.

var num = parseInt("010");
// Non-strict mode: the value is 8
Strict mode: the value is 10
Copy the code

The above is the difference between strict mode and normal mode of a simple introduction, I hope to help you, like remember to like ha.