JavaScript 中的严格模式
在 JavaScript 中,严格模式(Strict Mode)是在 ES5 (ECMAScript 2009) 中引入的。引入 “严格模式” 的目的是使 JavaScript 代码更安全。
'use strict' 文本表达式用于在 JavaScript 代码中添加 严格模式(Strict Mode) 。它从代码中删除了静默错误,例如你不能在没有声明的情况下使用变量,你不能修改对象的 readable 属性等。
启用严格模式(Strict Mode)
要 enble strcit 模式,您应该将以下文本表达式写入代码顶部 -
'use strict';
使用 'use strict' 指令启用 JavaScript 的严格模式。
为什么使用 Strict 模式?
在这里,我们列出了使用严格 JavaScript 模式的一些原因 -
- 错误预防 - 严格模式可防止开发人员在编写 JavaScript 代码时犯的常见错误,例如在不声明的情况下初始化变量或使用保留关键字作为标识符。
- 更安全的代码 − 严格模式可防止意外创建全局变量。此外,它不允许使用像 'with' 这样的语句,这可能会导致代码中的漏洞。
- 未来兼容性 − 您可以使用脚本模式将代码与 JavaScript 的未来版本保持一致。例如,当前版本的 JavaScript 不包含 'public' 等关键字,而是为将来的版本保留的。因此,从现在开始,严格模式将不允许将其用作标识符。
全局范围内的 Strict 模式
当您在 JavaScript 代码顶部添加 'use strict' 时;它对整个代码使用 strict 模式。
例在下面的示例中,我们定义了 'y' 变量并使用 50 对其进行初始化。该代码在输出中打印 'y' 的值。
此外,我们初始化了变量 'x' 但没有声明它。因此,它会在控制台中提供错误,并且不会打印输出。
简而言之,strict 模式不允许在没有声明的情况下使用变量。
<html>
<head>
<title> Using the strict mode gloablly </title>
</head>
<body>
<script>
"use strict";
let y = 50; // This is valid
document.write("The value of the X is: " + y);
x = 100; // This is not valid
document.write("The value of the X is: " + x);
</script>
</body>
</html>
本地范围内的 strict 模式
您还可以在特定函数中使用 “strict mode”。因此,它只会应用于函数范围。让我们借助一个例子来理解它。
例在下面的示例中,我们只在 test() 函数中使用了 'use strict' 字面量。因此,它仅从函数中删除异常错误。
下面的代码允许您初始化变量,而无需在函数外部声明变量,但不在函数内部声明变量。
<html>
<head>
<title> Using the strict mode gloablly </title>
</head>
<body>
<script>
x = 100; // This is valid
document.write("The value of the X is - " + x);
function test() {
"use strict";
y = 50; // This is not valid
document.write("The value of the y is: " + x);
}
test();
</script>
</body>
</html>
在 strict 模式下不应犯的错误
1. 如果不声明变量,就不能用值初始化变量。
<script>
'use strict';
num = 70.90; // This is invalid
</script>
2. 同样,如果不声明对象,就不能使用它。
<script>
'use strict';
numObj = {a: 89, b: 10.23}; // This is invalid
</script>
3. 您无法使用 delete 关键字删除对象。
<script>
'use strict';
let women = { name: "Aasha", age: 29 };
delete women; // This is invalid
</script>
4. 在 strict 模式下,您无法删除对象原型。
<script>
'use strict';
let women = { name: "Aasha", age: 29 };
delete women.prototype; // This is invalid
</script>
5. 不允许使用 delete 运算符删除函数。
<script>
'use strict';
function func() { }
delete func; // This is invalid
</script>
6. 您不能拥有具有重复参数值的函数。
<script>
'use strict';
function func(param1, param1, param2) {
// Function with 2 param1 is not allowed!
}
</script>
7. 您不能为变量分配八进制数。
<script>
'use strict';
let octal = 010; // Throws an error
</script>
8. 您不能使用转义字符。
<script>
'use strict';
let octal = \010; // Throws an error
</script>
9. 您不能使用 eval、arguments、public 等保留关键字作为标识符。
<script>
'use strict';
let public = 100; // Throws an error
</script>
10. 不能写入对象的 readable 属性。
<script>
'use strict';
let person = {};
Object.defineProperty(person, 'name', { value: "abc", writable: false });
obj1.name = "JavaScript"; // throws an error
</script>
11. 您不能为 getters 函数赋值。
<script>
'use strict';
let person = { get name() { return "JavaScript"; } };
obj1.name = "JavaScript"; // throws an error
</script>
12. 在 strict 模式下,当您在函数中使用 'this' 关键字时,它引用调用函数的引用对象。如果未指定 reference 对象,则它引用 undefined 值。
<script>
'use strict';
function test() {
console.log(this); // Undefined
}
test();
</script>
13. 您不能在 strict 模式下使用 'with' 语句。
<script>
'use strict';
with (Math) {x = sin(2)}; // This will throw an error
</script>
14. 出于安全原因,您不能使用 eval() 函数来声明变量。
<script>
'use strict';
eval("a = 8")
</script>
15. 您不能将关键字用作为将来保留的标识符。以下关键字保留给将来 -
- implements
- interface
- package
- private
- protected

