在 JavaScript 中 use strict
本教程将讨论 JavaScript 中的 use strict
特性。在这里,我们将通过不同的示例了解如何在 JavaScript 代码语句中创建和执行 use strict
关键字。
JavaScript 中的 use strict
在 JavaScript 版本 ECMAScript 5 中,严格模式是引入的一项新功能,它将帮助我们在严格的操作上下文中执行一组程序及其功能。我们 use strict
的上下文来限制执行各种操作并抛出更多异常。
我们通过语句 use strict
指示浏览器 use strict
模式。它是 JavaScript 的最小且更安全的功能集。
在 JavaScript 中实现 use strict
请记住,我们不能对用大括号括起来的块语句 use strict 模式。下面提到了两种 use strict 模式的方法:
- 我们可以在整个脚本的全局范围内使用它。
- 我们可以在个别功能中使用它。
在 JavaScript 中为整个脚本实现 use strict
在任何其他语句之前,我们将确切的语句 use strict
用于为整个脚本调用严格模式。
语法:
// strict mode syntax for Whole-script
'use strict';
let a = 'script for strict mode!';
为 JavaScript 中的单个函数实现 use strict
在函数体中,在任何其他语句之前,我们将确切的语句 use strict
用于调用函数的严格模式。
语法:
function strictFunction() {
// strict mode syntax for Function
'use strict';
function nestedFunction() {
return 'Javascript on DelftStack';
}
return 'strict mode functions! ' + nestedFunction();
}
function notStrictFunction() {
return 'non strict function';
}
严格模式示例
众所周知,错误输入变量名会在普通 JavaScript 中创建一个新的全局变量。在严格模式下,它会抛出一个错误。
例子:
<!DOCTYPE html>
<html>
<body>
<p>function will cause errors while using `use strict` in that function.</p>
<p>To see the error reprt just activate debugging in your browser by pressing (F12).</p>
<script>
a = 10; // This will not cause an error.
myFunction();
function myFunction() {
"use strict";
b = 10; // This will cause an error (b is not defined).
}
</script>
</body>
</html>
我们在这个 HTML 页面源代码中定义了 <script>
标记以使用 JavaScript 代码语句。在 <script>
标签内,我们简单地用数值 10 初始化未定义变量 a
并调用 myFunction()
。
之后,我们使用 use strict
关键字创建了 myFunction()
的声明。我们还用数值 10 初始化了未定义的变量 b
,以在 use strict
模式下测试执行。
你可以使用 .html 扩展名保存给定的 HTML 示例,并在任何浏览器中打开它以检查输出。你需要激活浏览器的调试模式,只需按 F12 即可查看错误报告。
输出:
the function will cause errors while using `use strict`.
To see the error report, activate debugging in your browser by pressing (F12).
在 JavaScript 中实现 use strict
的优势
在简单的 JavaScript 语义中,严格模式进行了各种更改。我们可以通过 use strict 模式将它们更改为抛出错误来消除 JavaScript 的一些静默错误。
- 为了执行优化,我们可以修复使 JavaScript 引擎难以处理的错误。
- 严格模式的代码有时可以比普通代码执行得更快,这是严格模式所没有的。
- 在 ECMAScript 的未来版本中,严格模式会阻止某些可能被定义的语法。
- 严格模式可防止任何不安全的操作,例如尝试访问全局变量或对象。
- 严格模式禁用令人困惑的功能或考虑不周的功能。
- 为了更有效地编写安全的 JavaScript,我们使用严格模式。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。