• Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

background

Has been code in the business, but neglect the most basic knowledge, I believe that many people have heard of strict mode and non-strict mode, but do not know the specific difference, through this article, we take you to understand and summarize the difference between strict mode and non-strict mode.

Strict mode

ES5 adds “strict mode” to the language, which severely restricts the rules for certain behaviors. In general, these restrictions keep code within a safer and more appropriate set of specifications. In addition, following a strict pattern makes it easier for the engine to optimize your code. Strict mode is a breakthrough in code that you should always use in your own programs.

If strict mode is turned on in code with “use strict”, the following cases raise [SyntaxError] before the script runs:

  • Var n = 023 and var s = “\047”

  • [with] statement

  • Use [delete] to delete a variable name (instead of an attribute name):delete myVariable

  • Use eval or arguments for variable or function names

  • Use future reserved words (perhaps in ECMAScript 6):implements, interface, let, package, private, protected, public, static, and yield for variable or function names

  • Use function declarations in statement blocks :if(a

  • Other errors

    • Object literals use two identical attribute names:{a: 1, b: 3, a: 7}
    • Use two identical parameter names in function parameters:function f(a, b, b){}

These errors are beneficial because they reveal crude errors and bad practices that are thrown before the code runs

variable

How and when variables are created in strict mode changes. The first change is to not allow accidental creation of global variables. In non-strict mode, the following code can create global variables:

// ReferenceError message = "Hello world!" ;Copy the code

object

In strict mode,. Attribute names must be unique when using object literals.

Let person = {name: "Nicholas", name: "Greg"};Copy the code

function

Function sum (num, num){// Function code}Copy the code

this

Null or undefined values are forced to be global objects in non-strict mode when using the apply() or call() methods of functions. In strict mode, the function this is always given the specified value, regardless of what value is specified. Such as:

// Access attributes // Non-strict mode: access global attributes // strict mode: throw an error because this value is null let color = "red"; function displayColor() { alert(this.color); } displayColor.call(null);Copy the code

Class and module

For classes, this includes class declarations and class expressions, with constructors, instance methods, static methods, get methods, and set methods all in strict mode. For modules, all code defined within them is in strict mode.

Other changes

There are other changes to be aware of in strict mode. The first is to eliminate the with statement. The with statement changes the way identifiers are parsed; this syntax has been dropped for simplicity in strict mode. Using with in strict mode causes syntax errors:

// Strict mode: raise SyntaxError with(location) {alert(href); }Copy the code

Strict mode also removes octal literals from JavaScript. Octal literals, which start with a leading 0, have been the source of many errors. Using octal literals in strict mode is considered invalid syntax:

// Use octal literals // Non-strict mode: the value is 8 // strict mode: throw SyntaxError let value = 010;Copy the code

ECMAScript 5 modifies parseInt() in non-strict mode to treat octal literals as decimal literals with leading zeros.

Let value = parseInt("010"); let value = parseInt("010");Copy the code

conclusion

Strict mode is recommended because it makes the code more formal and easier to maintain and troubleshoot later. More rigorous.