This article brings you relevant knowledge about JavaScript, which mainly introduces issues related to strict mode. Strict mode is easy to understand. It is a restrictive JavaScript mode, which makes the code implicitly break away from " "Lazy mode", let's take a look at it, I hope it will be helpful to everyone.
[Related recommendations: javascript video tutorial, web front-end]
In the ECMAScript5 standard, JavaScript proposed the concept of strict mode:
Strict mode is easy to understand, it is a restrictive JavaScript mode, which makes the code implicitly break out of "lazy mode".
When a browser that supports strict mode detects strict mode in the code, it will detect and execute the code in a more strict manner.
Strict mode imposes some restrictions on normal JavaScript semantics:
Strict mode eliminates some of the original silent errors by throwing them .
Strict mode allows the JS engine to perform more optimizations when executing code (without having to deal with some special syntax).
Strict mode disables some syntax that may be defined in future versions of ECMAScript.
So how to enable strict mode? Strict mode supports granular migration:
Can support turning on strict mode in js files;
It also supports turning on strict mode for a certain function;
JavaScript is designed to be easier for novice developers to use, so sometimes it has incorrect syntax, but don’t think it can be parsed normally;
However, this method may leave security risks;
In strict mode, this kind of transaction will be treated as an error so that it can be quickly discovered and corrected;
Here is a summary of some common limitations:
There are two ways to accidentally create global variables:
Globally declare global variables directly without using keywords
If you declare a variable without using keywords inside a function, the function will be promoted to a global variable by default. The example code is as follows:
'use strict' // Disable accidental creation of global variables message = 'Accidental creation of global variables' console.log(message) //The error message is: Uncaught ReferenceError: message is not defined function foo () { age=20 console.log(age) } foo() // ReferenceError: age is not defined
The sample code is as follows:
//Enable strict mode and convert the problem directly into an error 'use strict' const v = 100;//(define constant) v = 1.14; //Reassign value (to variable) console.log(v); // Uncaught TypeError: Assignment to constant variable.
In strict mode, you cannot use the deselect operator on variables.
// Turn on strict mode 'use strict' var v = 100; delete v;//In non-strict mode: this is a silent failure, neither an error is reported nor the variable v is deleted. console.log(v);//100 //After turning on strict mode, an error is reported instead Delete of an unqualified identifier in strict mode.
In strict mode, using the delete keyword for arrays and method attributes has the same effect.
// Turn on strict mode 'use strict' // 1. Delete the array content in strict mode var arr = [1,2,3,4] delete arr[0]; console.log(arr);//[ <1 empty item>, 2, 3, 4 ] // 2. Attributes of delete function in strict mode var obj = { name: 'Pig Man' } delete obj.name; console.log(obj.name)//undefined
The sample code is as follows:
'use strict' // Functions with the same parameter name are not allowed function foo2 (x, y, x) { console.log(x, y, x) } foo2(10, 20, 30) // SyntaxError: Duplicate parameter name not allowed in this context
The sample code is as follows:
// The original octal format var num1 = 0123 is not allowed var num2 = 0o123 // Can be written in this format as octal var num3 = 0x123 // Can be written in this format as hexadecimal var num4 = 0b100 // Can be written in this format as binary console.log(num1) // SyntaxError: Octal literals are not allowed in strict mode. console.log(num2, num3, num4) // 83 291 4
The sample code is as follows:
'use strict' var message = 'Hello World'; var obj = { name: 'jam', age: 20 } //The with statement can form its own scope. When printing age in the with statement, the age attribute in the obj object will be output. However, when the non-existent attribute message is printed in the with statement, it will be printed one level to the outer scope. One level to find function foo () { with (obj) { console.log(age) console.log(message) } console.log(message) } foo() // SyntaxError: Strict mode code may not include a with statement
The sample code is as follows:
var jsString = "var message = 'hello world';console.log(message)" eval(jsString) // Output hello world console.log(message) //The error message is: ReferenceError: message is not defined
开启严格模式eval函数不会向上引用变量所以全局作用域就没有message这个变量所以会报错
The sample code is as follows:
'use strict' // In strict mode, the self-executing function (default binding) will point to undefined, and in non-strict mode, it will point to window function foo () { console.log(this) } foo() // undefined