1. Overview 1.1 What is strict mode? Strict mode is a more restrictive variant of Although most browsers now support strict mode, some older versions of browsers still do not support strict mode, so do not use strict mode without testing the strict mode features. Strict mode and non-strict mode can coexist in 1.2 Purpose of strict modeThe purpose of using strict mode is as follows: First, strict mode turns 2. Enable strict mode There are two ways to enable strict mode in 2.1 Enable strict mode globallyTo enable global strict mode, you only need to enter a string before all the codes. The string is as follows: "use strict"; // or 'use strict'; It should be noted that if the previous 2.2 Locally enable strict mode To enable strict mode locally, you can add the string " The example code for enabling strict mode is as follows: //Globally enable strict mode //"use strict" v = 100 console.log(v) function fun() { // Locally enable strict mode 'use strict' vv = 200 console.log(vv) } // fun() throws an exception vv is not defined 3. Variables in strict mode 3.1 Prevent accidental creation of variablesThe so-called accidentally created variables are variables declared without using the var keyword. When in strict mode, if a global variable is accidentally created, an exception will be thrown. The sample code is as follows: 'use strict' // In non-strict mode, creating a variable like this will not report an error, but in strict mode, creating a variable like this will throw an exception v = 100 console.log(v) 3.2 Silent failure turns into exceptionSilent failure means no error and no effect. In strict mode, it will be turned into an exception. 3.3 Disable the delete keywordIn non-strict mode, using the delete keyword on a global variable will fail silently, while in strict mode, an exception will be thrown. The sample code is as follows 'use strict' var v = 100 delete v // SyntaxError: Deleteofanunqualifiedidentifierinstrictmode. console.log(v) 3.4 Restrictions on variable names In strict mode, 4. Objects in strict mode 4.1 Non-deletable attributes In non-strict mode, using the The sample code is as follows: "use strict" delete Object.prototype; //Throw an exception 4.2 Assignment of read-only propertiesIn non-strict mode, assigning a value to a read-only property will fail silently, but in strict mode an exception will be thrown. The sample code is as follows: 'use strict' var obj = {} Object.defineProperty(obj, 'name', { value: 'a bowl of porridge', }) obj.name = 'Yiwan Zhou' //Throws an exception 4.3 Non-extensible objectsAdding new properties to a non-extensible object will fail silently in non-strict mode, but will throw an exception in strict mode. The sample code is as follows: // Enable global strict mode 'use strict' var obj = {} //Make it non-extensible Object.preventExtensions(obj) //Extend the object attribute obj.name = 'Yiwan Zhou' //Throw an exception 5. Functions in strict mode 5.1 Parameter names must be uniqueIn non-strict mode, function parameters can be repeated, but in strict mode, if the function parameters are repeated, an exception will be thrown. The sample code is as follows: 'use strict' function fun(a, a, b) { console.log(a + a + b) } /* *The result in non-strict mode is 7=2+2+3 *An exception will be thrown in strict mode*/ fun(1, 2, 3) 5.2 Differences in arguments The differences are as follows:
The sample code is as follows: 'use strict' function fun(v) { v = '100' console.log(v) console.log(arguments[0]) } /* * The result printed in non-strict mode is 100, 100 *The results printed in strict mode are 100, 200 */ fun(200) 5.3 arguments.callee property In non-strict mode, you can use the The sample code is as follows: 'use strict' function fun(){ console.log(arguments.callee); } fun()//Throw an exception 5.4 Restrictions on Function DeclarationsIn strict mode, functions can only be declared in the global scope and local scope. It is an error to declare function syntax outside of these two scopes (such as if statement blocks). The sample code is as follows: 'use strict' function fun() { console.log(arguments.callee) } fun() //Throw an exception 6. Increase the scope of eval() In strict mode, variables created with the An exception will be thrown when used externally. The sample code is as follows: 'use strict' eval('var v=100') console.log(v) //Throw an exception 7. Suppress this When using the The sample code is as follows: // Enable strict mode 'use strict' var v = 100 function fn() { console.log(this.v) } var obj = { v: 200, } fn.call(obj) //this points to the global object Conclusion: This article basically introduces all the situations of strict mode, which is sufficient to solve the problems related to strict mode in daily development. This is the end of this article about You may also be interested in:
|
<<: Summary of the dockerfile-maven-plugin usage guide
>>: Pure CSS to adjust Div height according to adaptive width (percentage)
This article shares the specific code for impleme...
Like means "like" in Chinese, but when ...
Preface We have already installed Docker and have...
Let's take a look at the situation where Secu...
Introduction In a production environment, in orde...
Table of contents 1. Basic use of axio 2. How to ...
The TextBox with the ReadOnly attribute will be di...
After entering yum in linux, the prompt: -bash: /...
Problem [root@zh ~]# [root@zh ~]# [root@zh ~]# yu...
I believe everyone has played scratch tickets. Wh...
1. Mathematical Functions ABS(x) returns the abso...
Problem Description The button style is icon + te...
Must read before operation: Note: If you want to ...
When saving data in MySQL, sometimes some messy a...
String functions Check the ascii code value of th...