This is the 26th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021
Hello everyone, I am a bowl week, a front end that does not want to be drunk (inrolled). If I am lucky enough to write an article that you like, I am very lucky
An overview of the
What is the strict mode
Strict mode is one of the more restrictive variations in JavaScript. Strict mode is not a subset of JavaScript, and it is semantically distinct from normal code.
Although most browsers now support strict mode, some older browsers do not support strict mode, so do not use strict mode without testing the feature.
Strict and non-strict patterns can coexist in JavaScript, so you can selectively add strict patterns to your scripts.
The purpose of the strict pattern
The purpose of using strict mode is as follows:
- First, strict mode turns JavaScript traps into outright errors.
- Second, strict fixes some errors that the engine is hard to optimize: the same code can sometimes be faster in strict mode than in non-strict mode.
- Strict mode disables some syntax that may be defined in future versions.
Turn on strict mode
In JavaScript, strict mode can be opened in two ways: global strict mode and local strict mode.
The strict mode is enabled globally
To enable global strict mode, you simply type a string in front of all the code. The string looks like this:
"use strict";// or 'use strict';
Copy the code
It is important to note that if the previous JavaScript code is in non-strict mode, the setup is not to turn on strict mode for this code, which may cause problems. It is recommended to start strict mode locally and adjust the code step by step.
Locally turn on strict mode
To enable strict mode locally, add “use strict” to the first line of a specified function. This string, this function is still in non-strict mode.
Example code to enable strict mode looks like this:
// Enable strict mode globally
//"use strict"
v = 100
console.log(v)
function fun() {
// Enable strict mode locally
'use strict'
vv = 200
console.log(vv)
}
// fun() throws an exception vv is not defined
Copy the code
Variables in strict schema
Disallow accidental variable creation
Accidentally created variables are those declared without the var keyword. When in strict mode, an exception will be thrown if a global variable is accidentally created, as shown in the sample code below:
'use strict'
In non-strict mode, creating variables this way will not raise an error, but in strict mode creating variables this way will raise an exception
v = 100
console.log(v)
Copy the code
Silent failure becomes an exception
Silent failure is neither an error nor an effect, and in strict mode will become an exception.
The delete keyword is disabled
In non-strict mode, using the delete keyword on a global variable will result in a silent failure, while in strict mode, an exception will be thrown, as shown in the sample code below
'use strict'
var v = 100
delete v // SyntaxError:Deleteofanunqualifiedidentifierinstrictmode.
console.log(v)
Copy the code
Restrictions on variable names
JavaScript also has restrictions on variable names in strict mode. In particular, you can’t use reserved words as variable names. Using reserved words as characters can cause syntactic errors.
Objects in strict schema
An attribute that cannot be deleted
In non-strict mode, using the delete keyword on non-deletable attributes will cause a silent failure, while in strict mode, an exception will be thrown.
The sample code looks like this:
"use strict"
delete Object.prototype;// Throw an exception
Copy the code
Assignment of a read-only property
In non-strict mode, an assignment to a read-only property will silently fail, but in strict mode an exception will be thrown, as shown in the following code:
'use strict'
var obj = {}
Object.defineProperty(obj, 'name', {
value: 'A bowl of porridge',
})
obj.name = 'Bowl Week' // Throw an exception
Copy the code
Objects that are not extensible
In non-strict mode, adding new attributes to an unextensible object silently fails, while in strict mode an exception is thrown. The sample code looks like this:
// Enable global strict mode
'use strict'
var obj = {}
// Make 悐 unextensible
Object.preventExtensions(obj)
// Extend attributes for the object
obj.name = 'Bowl Week' // Throw an exception
Copy the code
Functions in strict schema
The parameter name must be unique
In non-strict mode, the parameters of a function can be repeated, but in strict mode, an exception will be thrown if the parameters of a function are repeated, as shown in the following example code:
'use strict'
function fun(a, a, b) {
console.log(a + a + b)
}
/* * In non-strict mode 7=2+2+3 * In strict mode an exception will be thrown */
fun(1.2.3)
Copy the code
The arguments of different
The arguments object behaves differently in strict mode and non-strict mode, as follows:
- In non-strict mode, changing the value of an argument is also reflected
arguments
In the object. - In strict mode, named parameters are completely separate from ‘arguments’ objects.
The sample code looks like this:
'use strict'
function fun(v) {
v = '100'
console.log(v)
console.log(arguments[0])}/* * The result printed in non-strict mode is 100,100 * 100,200 */ in strict mode
fun(200)
Copy the code
The arguments. The callee properties
In non-strict mode, you can use the arguments.callee property, which results in the current function name, whereas in strict mode, using this property will throw an exception.
Sample code is as follows
'usestrict'
functionfun(){
console.log(arguments.callee);
}
fun()// Throw an exception
Copy the code
Limitations on function declarations
In strict mode, functions can only be declared in global and local scopes, and it is wrong to declare functions outside these two scopes (for example, if statement blocks). The sample code looks like this:
'use strict'
function fun() {
console.log(arguments.callee)
}
fun() // Throw an exception
Copy the code
Add scope to eval()
In strict mode, variables created using the eval() function can only be used inside the function. External use will throw an exception, as shown in the following code:
'use strict'
eval('var v=100')
console.log(v) // Throw an exception
Copy the code
Inhibition of this
Null or undefined values are converted to global objects when Fucntion’s apply() or call() methods are used in non-strict mode. In strict mode, the function’s this value is always the specified value.
The sample code looks like this:
// 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
Copy the code
Write in the last
This article covers almost all of the scenarios for strict patterns, and it’s enough to address the problems of strict patterns in daily development.
This is the fifty-sixth article in the Learn from Scratch series, Strict Mode. If you like this column, please give me or the column a look
This series of articles in the nuggets first, preparation is not easy to reprint please obtain permission
Phase to recommend
- Summary of 42 front-end layout schemes – Juejin (juejin. Cn)
- JS advanced must be 5 high order functions – nuggets (juejin. Cn)
- 6 types of inheritance before ES6 – Digging gold (juejin.cn)
- – Juejin (juejin. Cn)