Sign in
Sign up
Explore
Enterprise
Education
Search
Help
Terms of use
About Us
Explore
Enterprise
Education
Gitee Premium
Gitee AI
AI teammates
Sign in
Sign up
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Cancel
Sign in
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
Watch
Unwatch
Watching
Releases Only
Ignoring
3
Star
47
Fork
21
DreamCoders
/
CoderGuide
Code
Issues
1169
Pull Requests
0
Wiki
Insights
Pipelines
Service
JavaDoc
PHPDoc
Quality Analysis
Jenkins for Gitee
Tencent CloudBase
Tencent Cloud Serverless
悬镜安全
Aliyun SAE
Codeblitz
SBOM
Don’t show this again
Update failed. Please try again later!
Remove this flag
Content Risk Flag
This task is identified by
as the content contains sensitive information such as code security bugs, privacy leaks, etc., so it is only accessible to contributors of this repository.
说说js 什么是严格模式,及严格模式的限制?
Backlog
#IAG9OU
陌生人
owner
Opened this issue
2024-07-29 16:07
<p style="text-align: justify;"><span style="font-size: 15px;">JavaScript的严格模式(Strict Mode)是一种在代码中启用的特殊模式,用于提供更严格的语法和错误检查,以改善代码质量和增强安全性。使用严格模式可以帮助大家避免一些常见的错误,并禁用一些不推荐使用的特性。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">要启用严格模式,可以在代码的顶部或函数体的开头添加以下语句:</span></p><pre><code class="language-javascript">"use strict";</code></pre><p style="text-align: justify;"><span style="font-size: 15px;">启用严格模式后,会应用一些限制和变化,包括以下几个方面:</span></p><p style="text-align: justify;"><span style="font-size: 15px;">1. 变量必须先声明后使用:在严格模式下,变量必须通过 var、let 或 const 关键字进行声明,否则会抛出 ReferenceError。在非严格模式下,未声明的变量会被隐式创建,并被添加到全局对象(比如浏览器环境中的 window 对象)中。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">2. 禁止删除变量、函数或函数参数:在严格模式下,使用 delete 操作符删除变量、函数或函数参数会抛出 SyntaxError。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">3. 禁止对只读属性进行赋值:在严格模式下,对只读属性(通过 const 关键字声明的常量)进行赋值会抛出 TypeError。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">4. 禁止使用八进制字面量:在严格模式下,以 0 开头的数字会被视为八进制字面量,这在非严格模式下是允许的。严格模式下,使用八进制字面量会抛出 SyntaxError。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">5. 限制 this 值:在严格模式下,函数内部的 this 值不再是全局对象(比如浏览器环境中的 window 对象),而是undefined,除非通过 call()、apply() 或 bind() 明确指定。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">6. 禁止使用重复的函数参数名:在严格模式下,函数参数名不能重复。在非严格模式下,重复的函数参数名会被忽略。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">7. 禁止使用 with 语句:在严格模式下,使用 with 语句会抛出 SyntaxError。with 语句在非严格模式下允许将对象的属性添加到作用域链中,但这被认为是不推荐使用的特性,因为它可能导致代码可读性和性能问题。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">8. 限制 eval 和 arguments 的赋值:在严格模式下,无法对 eval 和 arguments 进行赋值。在非严格模式下,这种赋值是允许的。</span></p><p style="text-align: justify;"><br></p><p style="text-align: justify;"><span style="font-size: 15px;">下面是一些使用JavaScript严格模式的例子,展示了严格模式下的限制和行为变化:</span></p><p style="text-align: justify;"><span style="font-size: 15px;">1. 变量必须先声明后使用:</span></p><pre><code class="language-javascript">"use strict"; x = 10; // 抛出 ReferenceError: x is not defined</code></pre><p style="text-align: justify;"><span style="font-size: 15px;">在严格模式下,对未声明的变量进行赋值会抛出错误。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">2. 禁止删除变量:</span></p><pre><code class="language-javascript">"use strict"; var x = 10; delete x; // 抛出 SyntaxError: Delete of an unqualified identifier in strict mode.</code></pre><p style="text-align: justify;"><span style="font-size: 15px;">在严格模式下,无法使用 delete 操作符删除变量。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">3. 禁止对只读属性进行赋值:</span></p><pre><code class="language-javascript">"use strict"; var obj = {}; Object.defineProperty(obj, "x", { value: 10, writable: false }); obj.x = 20; // 抛出 TypeError: Cannot assign to read only property 'x' of object ' <Object>'</code></pre><p style="text-align: justify;"><span style="font-size: 15px;">在严格模式下,对只读属性进行赋值会抛出错误。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">4. 八进制字面量的限制:</span></p><pre><code class="language-javascript">"use strict"; var num = 012; // 抛出 SyntaxError: Octal literals are not allowed in strict mode.</code></pre><p style="text-align: justify;"><span style="font-size: 15px;">在严格模式下,使用八进制字面量会被视为语法错误。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">5. 函数中的 this 值为 undefined:</span></p><pre><code class="language-javascript">"use strict"; function showThis() { console.log(this); } showThis(); // undefined </code></pre><p style="text-align: justify;"><span style="font-size: 15px;">在严格模式下,如果函数内部的 this 值未明确指定,它将保持为 undefined。</span><br><span style="font-size: 15px;">这些是严格模式下的一些限制和变化,它们有助于提高代码的可靠性和可维护性。在编写新代码或更新现有代码时,启用严格模式是一种良好的实践,可以帮助捕获潜在的错误并遵循更严格的编码标准。</span></p><p><br></p>
<p style="text-align: justify;"><span style="font-size: 15px;">JavaScript的严格模式(Strict Mode)是一种在代码中启用的特殊模式,用于提供更严格的语法和错误检查,以改善代码质量和增强安全性。使用严格模式可以帮助大家避免一些常见的错误,并禁用一些不推荐使用的特性。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">要启用严格模式,可以在代码的顶部或函数体的开头添加以下语句:</span></p><pre><code class="language-javascript">"use strict";</code></pre><p style="text-align: justify;"><span style="font-size: 15px;">启用严格模式后,会应用一些限制和变化,包括以下几个方面:</span></p><p style="text-align: justify;"><span style="font-size: 15px;">1. 变量必须先声明后使用:在严格模式下,变量必须通过 var、let 或 const 关键字进行声明,否则会抛出 ReferenceError。在非严格模式下,未声明的变量会被隐式创建,并被添加到全局对象(比如浏览器环境中的 window 对象)中。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">2. 禁止删除变量、函数或函数参数:在严格模式下,使用 delete 操作符删除变量、函数或函数参数会抛出 SyntaxError。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">3. 禁止对只读属性进行赋值:在严格模式下,对只读属性(通过 const 关键字声明的常量)进行赋值会抛出 TypeError。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">4. 禁止使用八进制字面量:在严格模式下,以 0 开头的数字会被视为八进制字面量,这在非严格模式下是允许的。严格模式下,使用八进制字面量会抛出 SyntaxError。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">5. 限制 this 值:在严格模式下,函数内部的 this 值不再是全局对象(比如浏览器环境中的 window 对象),而是undefined,除非通过 call()、apply() 或 bind() 明确指定。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">6. 禁止使用重复的函数参数名:在严格模式下,函数参数名不能重复。在非严格模式下,重复的函数参数名会被忽略。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">7. 禁止使用 with 语句:在严格模式下,使用 with 语句会抛出 SyntaxError。with 语句在非严格模式下允许将对象的属性添加到作用域链中,但这被认为是不推荐使用的特性,因为它可能导致代码可读性和性能问题。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">8. 限制 eval 和 arguments 的赋值:在严格模式下,无法对 eval 和 arguments 进行赋值。在非严格模式下,这种赋值是允许的。</span></p><p style="text-align: justify;"><br></p><p style="text-align: justify;"><span style="font-size: 15px;">下面是一些使用JavaScript严格模式的例子,展示了严格模式下的限制和行为变化:</span></p><p style="text-align: justify;"><span style="font-size: 15px;">1. 变量必须先声明后使用:</span></p><pre><code class="language-javascript">"use strict"; x = 10; // 抛出 ReferenceError: x is not defined</code></pre><p style="text-align: justify;"><span style="font-size: 15px;">在严格模式下,对未声明的变量进行赋值会抛出错误。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">2. 禁止删除变量:</span></p><pre><code class="language-javascript">"use strict"; var x = 10; delete x; // 抛出 SyntaxError: Delete of an unqualified identifier in strict mode.</code></pre><p style="text-align: justify;"><span style="font-size: 15px;">在严格模式下,无法使用 delete 操作符删除变量。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">3. 禁止对只读属性进行赋值:</span></p><pre><code class="language-javascript">"use strict"; var obj = {}; Object.defineProperty(obj, "x", { value: 10, writable: false }); obj.x = 20; // 抛出 TypeError: Cannot assign to read only property 'x' of object ' <Object>'</code></pre><p style="text-align: justify;"><span style="font-size: 15px;">在严格模式下,对只读属性进行赋值会抛出错误。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">4. 八进制字面量的限制:</span></p><pre><code class="language-javascript">"use strict"; var num = 012; // 抛出 SyntaxError: Octal literals are not allowed in strict mode.</code></pre><p style="text-align: justify;"><span style="font-size: 15px;">在严格模式下,使用八进制字面量会被视为语法错误。</span></p><p style="text-align: justify;"><span style="font-size: 15px;">5. 函数中的 this 值为 undefined:</span></p><pre><code class="language-javascript">"use strict"; function showThis() { console.log(this); } showThis(); // undefined </code></pre><p style="text-align: justify;"><span style="font-size: 15px;">在严格模式下,如果函数内部的 this 值未明确指定,它将保持为 undefined。</span><br><span style="font-size: 15px;">这些是严格模式下的一些限制和变化,它们有助于提高代码的可靠性和可维护性。在编写新代码或更新现有代码时,启用严格模式是一种良好的实践,可以帮助捕获潜在的错误并遵循更严格的编码标准。</span></p><p><br></p>
Comments (
0
)
Sign in
to comment
Status
Backlog
Backlog
Doing
Done
Closed
Assignees
Not set
Labels
Html/JS/CSS
Not set
Label settings
Milestones
No related milestones
No related milestones
Pull Requests
None yet
None yet
Successfully merging a pull request will close this issue.
Branches
No related branch
No related branch
master
Planed to start   -   Planed to end
-
Top level
Not Top
Top Level: High
Top Level: Medium
Top Level: Low
Priority
Not specified
Serious
Main
Secondary
Unimportant
参与者(1)
1
https://gitee.com/DreamCoders/CoderGuide.git
git@gitee.com:DreamCoders/CoderGuide.git
DreamCoders
CoderGuide
CoderGuide
Going to Help Center
Search
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register