Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AutoCorrect files/zh-cn/web/javascript/reference #5876

Merged
merged 1 commit into from
May 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ <h2 id="描述">描述</h2>

<h3 id="块级作用域">块级作用域</h3>

<h4 id="在非严格模式non-strict_mode下的var_或者函数声明时">在非严格模式(non-strict mode)下的<code>var </code>或者函数声明时</h4>
<h4 id="在非严格模式non-strict_mode下的var_或者函数声明时">在非严格模式 (non-strict mode) 下的<code>var </code>或者函数声明时</h4>

<p>通过<code>var</code>声明的变量或者非严格模式下(non-strict mode)创建的函数声明<strong>没有</strong>块级作用域。在语句块里声明的变量的作用域不仅是其所在的函数或者 script 标签内,所设置变量的影响会在超出语句块本身之外持续存在。 换句话说,这种语句块不会引入一个作用域。尽管单独的语句块是合法的语句,但在JavaScript中你不会想使用单独的语句块,因为它们不像你想象的C或Java中的语句块那样处理事物。例如:</p>
<p>通过<code>var</code>声明的变量或者非严格模式下 (non-strict mode) 创建的函数声明<strong>没有</strong>块级作用域。在语句块里声明的变量的作用域不仅是其所在的函数或者 script 标签内,所设置变量的影响会在超出语句块本身之外持续存在。 换句话说,这种语句块不会引入一个作用域。尽管单独的语句块是合法的语句,但在 JavaScript 中你不会想使用单独的语句块,因为它们不像你想象的 C 或 Java 中的语句块那样处理事物。例如:</p>

<pre class="brush: js example-bad">var x = 1;
{
Expand All @@ -49,7 +49,7 @@ <h4 id="在非严格模式non-strict_mode下的var_或者函数声明时">在非
console.log(x); // 输出 2
</pre>

<p>输出结果是 2,因为块中的 <code>var x</code>语句与块前面的<code>var x</code>语句作用域相同。在 C 或 Java中,这段代码会输出 1。</p>
<p>输出结果是 2,因为块中的 <code>var x</code>语句与块前面的<code>var x</code>语句作用域相同。在 C 或 Java 中,这段代码会输出 1。</p>



Expand All @@ -71,11 +71,11 @@ <h4 id="使用let和_const">使用<code>let</code>和 <code>const</code></h4>
{
const c = 2;
}
console.log(c); // 输出1, 而且不会报错</pre>
console.log(c); // 输出 1,而且不会报错</pre>

<p>注意,位于块范围之内的 <code>const c = 2</code> 并不会抛出<code>SyntaxError: Identifier 'c' has already been declared</code>这样的语法错误,因为在它自己的块中它可能是唯一一个被声明的常量。</p>

<h5 id="使用let声明的变量在块级作用域内能强制执行更新变量,下面的两个例子对比:">使用let声明的变量在块级作用域内能强制执行更新变量,下面的两个例子对比:</h5>
<h5 id="使用let声明的变量在块级作用域内能强制执行更新变量,下面的两个例子对比:">使用 let 声明的变量在块级作用域内能强制执行更新变量,下面的两个例子对比:</h5>

<pre class="brush: js">var a = [];
for (<strong>var</strong> i = 0; i &lt; 10; i++) {
Expand All @@ -97,7 +97,7 @@ <h5 id="使用let声明的变量在块级作用域内能强制执行更新变量

<h4 id="使用function">使用<code>function</code></h4>

<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function">函数声明</a>同样被限制在声明他的语句块内:</p>
<p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function">函数声明</a>同样被限制在声明他的语句块内</p>



Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h2 id="描述">描述</h2>

<p><code>break</code>语句包含一个可选的标签,可允许程序摆脱一个被标记的语句。<code>break</code>语句需要内嵌在引用的标签中。被标记的语句可以是任何 {{jsxref("Statements/block", "块")}}语句;不一定是循环语句。</p>

<p>break语句不能在function函数体中直接使用,break语句应嵌套在要中断的当前循环、switch或label语句中。</p>
<p>break 语句不能在 function 函数体中直接使用,break 语句应嵌套在要中断的当前循环、switch 或 label 语句中。</p>

<h2 id="示例">示例</h2>

Expand All @@ -48,7 +48,7 @@ <h3 id="break_in_while_loop">break in while loop</h3>

<h3 id="break_in_switch_statements">break in switch statements</h3>

<p>在下面的代码中, <code>break</code> 使用在 {{jsxref("Statements/switch", "switch")}} 语句中,当遇到匹配到case后,就会执行相应的代码并中断循环体。</p>
<p>在下面的代码中, <code>break</code> 使用在 {{jsxref("Statements/switch", "switch")}} 语句中,当遇到匹配到 case 后,就会执行相应的代码并中断循环体。</p>

<pre>const food = "sushi";

Expand Down Expand Up @@ -96,7 +96,7 @@ <h3 id="break_in_labeled_blocks_that_throw">break in labeled blocks that throw</

<h3 id="break_within_functions">break within functions</h3>

<p>在下面的代码同样会产生SyntaxError,因为它并没被正确的使用在循环、switch或label语句中。</p>
<p>在下面的代码同样会产生 SyntaxError,因为它并没被正确的使用在循环、switch 或 label 语句中。</p>

<pre>function testBreak(x) {
var i = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ <h2 id="示例">示例</h2>

<h3 id="声明一个类">声明一个类</h3>

<p>在下面的例子中,我们首先定义一个名为Polygon的类,然后继承它来创建一个名为Square的类。注意,构造函数中使用的 super() 只能在构造函数中使用,并且必须在使用 this 关键字前调用。</p>
<p>在下面的例子中,我们首先定义一个名为 Polygon 的类,然后继承它来创建一个名为 Square 的类。注意,构造函数中使用的 super() 只能在构造函数中使用,并且必须在使用 this 关键字前调用。</p>

<pre class="brush: js">class Polygon {
constructor(height, width) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h3 id="const_基本用法">const 基本用法</h3>

<p>常量在声明的时候可以使用大小写,但通常情况下全部用大写字母。</p>

<pre class="brush: js;">// 定义常量MY_FAV并赋值7
<pre class="brush: js;">// 定义常量 MY_FAV 并赋值 7
const MY_FAV = 7;

// 报错 - Uncaught TypeError: Assignment to constant variable.
Expand Down Expand Up @@ -81,7 +81,7 @@ <h3 id="块作用域">块作用域</h3>
var MY_FAV = 20;
}

// MY_FAV 依旧为7
// MY_FAV 依旧为 7
console.log('my favorite number is ' + MY_FAV);
</pre>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ <h2 id="Examples">示例</h2>

<h3 id="Example_Using_continue_with_while">在 <code>while</code> 语句中使用 <code>continue</code></h3>

<p>下述例子展示了一个在<code>i</code> 为 3时执行<code>continue</code> 语句的 {{jsxref("Statements/while", "while")}} 循环。因此,<code>n</code> 的值在几次迭代后分别为 1, 3, 7 和 12 .</p>
<p>下述例子展示了一个在<code>i</code> 为 3 时执行<code>continue</code> 语句的 {{jsxref("Statements/while", "while")}} 循环。因此,<code>n</code> 的值在几次迭代后分别为 1, 3, 7 和 12 .</p>

<pre class="brush: js">i = 0;
n = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h2 id="示例">示例</h2>
// do potentially buggy stuff to examine, step through, etc.
}</pre>

<p>当 debugger 被调用时, 执行暂停在 debugger 语句的位置。就像在脚本源代码中的断点一样。</p>
<p>当 debugger 被调用时执行暂停在 debugger 语句的位置。就像在脚本源代码中的断点一样。</p>

<p><a href="https://mdn.mozillademos.org/files/6963/Screen%20Shot%202014-02-07%20at%209.14.35%20AM.png"><img alt="Paused at a debugger statement." src="https://mdn.mozillademos.org/files/6963/Screen%20Shot%202014-02-07%20at%209.14.35%20AM.png"></a></p>

Expand Down Expand Up @@ -72,6 +72,6 @@ <h2 id="浏览器兼容">浏览器兼容</h2>
<h2 id="See_also">相关链接</h2>

<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Debugging_JavaScript">JavaScript调试</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Debugging_JavaScript">JavaScript 调试</a></li>
<li><a href="https://developer.mozilla.org/en-US/docs/Tools/Debugger">在火狐开发者工具中调试</a></li>
</ul>
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
---
<div>{{jsSidebar("Statements")}}</div>

<p>在创建JavaScript模块时,<code><strong>export</strong></code> 语句用于从模块中导出实时绑定的函数、对象或原始值,以便其他程序可以通过 {{jsxref("Statements/import", "import")}} 语句使用它们。被导出的绑定值依然可以在本地进行修改。在使用import进行导入时,这些绑定值只能被导入模块所读取,但在export导出模块中对这些绑定值进行修改,所修改的值也会实时地更新。</p>
<p>在创建 JavaScript 模块时,<code><strong>export</strong></code> 语句用于从模块中导出实时绑定的函数、对象或原始值,以便其他程序可以通过 {{jsxref("Statements/import", "import")}} 语句使用它们。被导出的绑定值依然可以在本地进行修改。在使用 import 进行导入时,这些绑定值只能被导入模块所读取,但在 export 导出模块中对这些绑定值进行修改,所修改的值也会实时地更新。</p>

<p>无论您是否声明,导出的模块都处于{{jsxref("Strict_mode","严格模式")}}。 export语句不能用在嵌入式脚本中。</p>
<p>无论您是否声明,导出的模块都处于{{jsxref("Strict_mode","严格模式")}}。 export 语句不能用在嵌入式脚本中。</p>

<h2 id="语法">语法</h2>

Expand Down Expand Up @@ -66,7 +66,7 @@ <h2 id="描述">描述</h2>
<pre class="brush: js">// 导出事先定义的特性
export { myFunction, myVariable };

// 导出单个特性(可以导出var,let,
// 导出单个特性(可以导出 var,let,
//const,function,class)
export let myVariable = Math.sqrt(2);
export function myFunction() { ... };</pre>
Expand Down Expand Up @@ -118,7 +118,7 @@ <h3 id="重导出_聚合">重导出 / 聚合</h3>
<p>但这里的 <code>function1</code> 和 <code>function2</code> 在当前模块中变得不可用。</p>

<div class="note">
<p><strong>备注:</strong>尽管与import等效,但以下语法在语法上无效:</p>
<p><strong>备注:</strong>尽管与 import 等效,但以下语法在语法上无效:</p>
</div>

<pre class="brush: js">import DefaultExport from 'bar.js'; // 有效的
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ <h3 id="迭代异步可迭代对象">迭代异步可迭代对象</h3>

<h3 id="迭代异步生成器">迭代异步生成器 </h3>

<p>异步生成器已经实现了异步迭代器协议, 所以可以用 <code>for await...of</code>循环。</p>
<p>异步生成器已经实现了异步迭代器协议所以可以用 <code>for await...of</code>循环。</p>

<pre class="brush: js">async function* asyncGenerator() {
var i = 0;
Expand All @@ -92,7 +92,7 @@ <h3 id="迭代异步生成器">迭代异步生成器 </h3>
// 1
// 2</pre>

<p>有关使用<code>for await... of</code>考虑迭代API中获取数据的异步 generator 更具体的例子。这个例子首先为一个数据流创建了一个异步 generator,然后使用它来获得这个API的响应值的大小。</p>
<p>有关使用<code>for await... of</code>考虑迭代 API 中获取数据的异步 generator 更具体的例子。这个例子首先为一个数据流创建了一个异步 generator,然后使用它来获得这个 API 的响应值的大小。</p>

<pre class="brush: js">async function* streamAsyncIterator(stream) {
const reader = stream.getReader();
Expand All @@ -108,12 +108,12 @@ <h3 id="迭代异步生成器">迭代异步生成器 </h3>
reader.releaseLock();
}
}
// 从url获取数据并使用异步 generator 来计算响应值的大小
// 从 url 获取数据并使用异步 generator 来计算响应值的大小
async function getResponseSize(url) {
const response = await fetch(url);
// Will hold the size of the response, in bytes.
let responseSize = 0;
// 使用for-await-of循环. 异步 generator 会遍历响应值的每一部分
// 使用 for-await-of 循环。异步 generator 会遍历响应值的每一部分
for await (const chunk of streamAsyncIterator(response.body)) {
// Incrementing the total response length.
responseSize += chunk.length;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ <h2 id="Syntax">语法</h2>

<dl>
<dt><code>variable</code></dt>
<dd>在每次迭代时,variable会被赋值为不同的属性名。</dd>
<dd>在每次迭代时,variable 会被赋值为不同的属性名。</dd>
<dt><code>object</code></dt>
<dd>非Symbol类型的可枚举属性被迭代的对象。</dd>
<dd>非 Symbol 类型的可枚举属性被迭代的对象。</dd>
</dl>

<h2 id="sect1"></h2>
Expand All @@ -34,7 +34,7 @@ <h3 id="仅迭代自身的属性">仅迭代自身的属性</h3>

<p>如果你只要考虑对象本身的属性,而不是它的原型,那么使用 {{jsxref("Object.getOwnPropertyNames", "getOwnPropertyNames()")}} 或执行 {{jsxref("Object.prototype.hasOwnProperty", "hasOwnProperty()")}} 来确定某属性是否是对象本身的属性(也能使用{{jsxref("Object.prototype.propertyIsEnumerable", "propertyIsEnumerable")}})。或者,如果你知道不会有任何外部代码干扰,您可以使用检查方法扩展内置原型。</p>

<h2 id="Example">为什么用for ... in?</h2>
<h2 id="Example">为什么用 for ... in?</h2>

<p><code>for ... in</code>是为遍历对象属性而构建的,不建议与数组一起使用,数组可以用<code>Array.prototype.forEach()</code>和<code>for ... of</code>,那么<code>for ... in</code>的到底有什么用呢?</p>

Expand Down Expand Up @@ -130,7 +130,7 @@ <h2 id="兼容性:初始化函数表达式">兼容性:初始化函数表达
// 3
</pre>

<p>这个非标准行为现在在版本40及更高版本中被忽略,并将在严格模式({{bug(748550)}} 和 {{bug(1164741)}})中呈现{{jsxref("SyntaxError")}}("<a href="/zh-CN/docs/Web/JavaScript/Reference/Errors/Invalid_for-in_initializer">for-in loop head declarations may not have initializers</a>")错误。</p>
<p>这个非标准行为现在在版本 40 及更高版本中被忽略,并将在严格模式({{bug(748550)}} 和 {{bug(1164741)}})中呈现{{jsxref("SyntaxError")}}("<a href="/zh-CN/docs/Web/JavaScript/Reference/Errors/Invalid_for-in_initializer">for-in loop head declarations may not have initializers</a>")错误。</p>

<p>像其他引擎 V8(Chrome),Chakra (IE/Edge), JSC (WebKit/Safari) 正在研究去除这种不标准的行为。</p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ <h3 id="迭代_DOM_集合">迭代 DOM 集合</h3>

<p>迭代 DOM 元素集合,比如一个{{domxref("NodeList")}}对象:下面的例子演示给每一个 article 标签内的 p 标签添加一个 "<code>read</code>" 类。</p>

<pre class="brush:js">//注意:这只能在实现了NodeList.prototype[Symbol.iterator]的平台上运行
<pre class="brush:js">//注意:这只能在实现了 NodeList.prototype[Symbol.iterator] 的平台上运行
let articleParagraphs = document.querySelectorAll("article &gt; p");

for (let paragraph of articleParagraphs) {
Expand Down Expand Up @@ -164,7 +164,7 @@ <h3 id="迭代生成器">迭代生成器</h3>

for (let n of fibonacci()) {
     console.log(n);
    // 当n大于1000时跳出循环
    // 当 n 大于 1000 时跳出循环
    if (n &gt;= 1000)
        break;
}</pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ <h2 id="语法">语法</h2>
<dt><code>final-expression</code></dt>
<dd>每次循环的最后都要执行的表达式。执行时机是在下一次 <code>condition</code> 的计算之前。通常被用于更新或者递增计数器变量。</dd>
<dt><code>statement</code></dt>
<dd>只要<code>condition</code>的结果为true就会被执行的语句。要在循环体内执行多条语句,使用一个<a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/block">块语句</a>(<code>{ ... }</code>)来包含要执行的语句。没有任何语句要执行,使用一个<a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/Empty">空语句</a>(<code>;</code>)。</dd>
<dd>只要<code>condition</code>的结果为 true 就会被执行的语句。要在循环体内执行多条语句,使用一个<a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/block">块语句</a>(<code>{ ... }</code>)来包含要执行的语句。没有任何语句要执行,使用一个<a href="/zh-CN/docs/Web/JavaScript/Reference/Statements/Empty">空语句</a>(<code>;</code>)。</dd>
</dl>

<h2 id="示例">示例</h2>
Expand Down
Loading