From d9e9adb5f80a819fe46349bcf6d1faec734b09cd Mon Sep 17 00:00:00 2001
From: Irvin 下面的函数里有个 下述例子展示了一个在break
语句,当 i
为 3 时,会中止 {{jsxref("Statements/while", "while")}} 循环,然后返回 3 * x
的值。
+function testBreak(x) {
- var i = 0;
-
- while (i < 6) {
- if (i == 3) {
- break;
- }
- i += 1;
- }
-
- return i * x;
-}
function testBreak(x) {
+ var i = 0;
+
+ while (i < 6) {
+ if (i == 3) {
+ break;
+ }
+ i += 1;
+ }
+
+ return i * x;
+}
break in switch statements
diff --git a/files/zh-cn/web/javascript/reference/statements/continue/index.html b/files/zh-cn/web/javascript/reference/statements/continue/index.html
index dfea4a3be3..ce08e7f444 100644
--- a/files/zh-cn/web/javascript/reference/statements/continue/index.html
+++ b/files/zh-cn/web/javascript/reference/statements/continue/index.html
@@ -43,15 +43,15 @@ translation_of: Web/JavaScript/Reference/Statements/continue
i
为 3时执行continue
语句的 {{jsxref("Statements/while", "while")}} 循环。因此,n
的值在几次迭代后分别为 1, 3, 7 和 12 .
+i = 0;
-n = 0;
-while (i < 5) {
- i++;
- if (i === 3) {
- continue;
- }
- n += i;
-}
i = 0;
+n = 0;
+while (i < 5) {
+ i++;
+ if (i === 3) {
+ continue;
+ }
+ n += i;
+}
使用带 label 的 continue
@@ -61,51 +61,51 @@ n = 0参考 {{jsxref("Statements/label", "label")}} 。
var i = 0,
- j = 8;
+var i = 0,
+ j = 8;
-checkiandj: while (i < 4) {
- console.log("i: " + i);
- i += 1;
+checkiandj: while (i < 4) {
+ console.log("i: " + i);
+ i += 1;
- checkj: while (j > 4) {
- console.log("j: "+ j);
- j -= 1;
- if ((j % 2) == 0)
- continue checkj;
- console.log(j + " is odd.");
- }
- console.log("i = " + i);
- console.log("j = " + j);
-}
+ checkj: while (j > 4) {
+ console.log("j: "+ j);
+ j -= 1;
+ if ((j % 2) == 0)
+ continue checkj;
+ console.log(j + " is odd.");
+ }
+ console.log("i = " + i);
+ console.log("j = " + j);
+}
输出:
-"i: 0"
-
+"i: 0"
+
// start checkj
-
"j: 8"
-"7 is odd."
-"j: 7"
-"j: 6"
-"5 is odd."
-"j: 5"
+"j: 8"
+"7 is odd."
+"j: 7"
+"j: 6"
+"5 is odd."
+"j: 5"
// end checkj
-
-"i = 1"
-"j = 4"
-"i: 1"
-"i = 2"
-"j = 4"
+"i = 1"
+"j = 4"
+
+"i: 1"
+"i = 2"
+"j = 4"
-"i: 2"
-"i = 3"
-"j = 4"
+"i: 2"
+"i = 3"
+"j = 4"
-"i: 3"
-"i = 4"
-"j = 4"
+"i: 3"
+"i = 4"
+"j = 4"
下面的例子演示了一个包含 debugger 语句的函数,当函数被调用时,会尝试调用一个可用的调试器进行调试。
-function potentiallyBuggyCode() {
- debugger;
- // do potentially buggy stuff to examine, step through, etc.
-}
+function potentiallyBuggyCode() {
+ debugger;
+ // do potentially buggy stuff to examine, step through, etc.
+}
当 debugger 被调用时, 执行暂停在 debugger 语句的位置。就像在脚本源代码中的断点一样。
diff --git a/files/zh-cn/web/javascript/reference/statements/do...while/index.html b/files/zh-cn/web/javascript/reference/statements/do...while/index.html index bd3fb46473..714c0e807e 100644 --- a/files/zh-cn/web/javascript/reference/statements/do...while/index.html +++ b/files/zh-cn/web/javascript/reference/statements/do...while/index.html @@ -10,7 +10,7 @@ translation_of: Web/JavaScript/Reference/Statements/do...whiledo...while
语句创建一个执行指定语句的循环,直到condition
值为 false。在执行statement
后检测condition
,所以指定的statement
至少执行一次。
do...while
语句创建一个执行指定语句的循环,直到condition
值为 false。在执行statement
后检测condition
,所以指定的statement
至少执行一次。
statement
condition
值为真时重新执行。想执行多行语句,可使用{{jsxref("Statements/block", "block")}}语句({ ... }
)包裹这些语句。condition
值为真时重新执行。想执行多行语句,可使用{{jsxref("Statements/block", "block")}}语句({ ... }
)包裹这些语句。condition
condition
值为真, statement
会再次执行。当 condition
值为假,则跳到do...while
之后的语句。condition
值为真, statement
会再次执行。当 condition
值为假,则跳到do...while
之后的语句。<div id="example"></div>
+<div id="example"></div>
var result = '';
-var i = 0;
-do {
- i += 1;
- result += i + ' ';
-} while (i < 5);
-document.getElementById('example').innerHTML = result;
+var result = '';
+var i = 0;
+do {
+ i += 1;
+ result += i + ' ';
+} while (i < 5);
+document.getElementById('example').innerHTML = result;
空语句是一个分号(;),表示不会执行任何语句,即使 JavaScript 语法需要一个语句。 相反,当你需要多行语句,但 JavaScript 只允许一个时,可以使用语句块;语句块可以将多条语句合并为一个。
+空语句是一个分号(;),表示不会执行任何语句,即使 JavaScript 语法需要一个语句。 相反,当你需要多行语句,但 JavaScript 只允许一个时,可以使用语句块;语句块可以将多条语句合并为一个。
空语句有时与循环语句一起使用。以下示例使用空循环体:
+空语句有时与循环语句一起使用。以下示例使用空循环体:
var arr = [1, 2, 3]; diff --git a/files/zh-cn/web/javascript/reference/statements/export/index.html b/files/zh-cn/web/javascript/reference/statements/export/index.html index b97bb5c967..2eb0086a2f 100644 --- a/files/zh-cn/web/javascript/reference/statements/export/index.html +++ b/files/zh-cn/web/javascript/reference/statements/export/index.html @@ -11,7 +11,7 @@ translation_of: Web/JavaScript/Reference/Statements/export ----{{jsSidebar("Statements")}}-在创建JavaScript模块时,
+export
语句用于从模块中导出实时绑定的函数、对象或原始值,以便其他程序可以通过 {{jsxref("Statements/import", "import")}} 语句使用它们。被导出的绑定值依然可以在本地进行修改。在使用import进行导入时,这些绑定值只能被导入模块所读取,但在export导出模块中对这些绑定值进行修改,所修改的值也会实时地更新。在创建JavaScript模块时,
export
语句用于从模块中导出实时绑定的函数、对象或原始值,以便其他程序可以通过 {{jsxref("Statements/import", "import")}} 语句使用它们。被导出的绑定值依然可以在本地进行修改。在使用import进行导入时,这些绑定值只能被导入模块所读取,但在export导出模块中对这些绑定值进行修改,所修改的值也会实时地更新。无论您是否声明,导出的模块都处于{{jsxref("Strict_mode","严格模式")}}。 export语句不能用在嵌入式脚本中。
diff --git a/files/zh-cn/web/javascript/reference/statements/for-await...of/index.html b/files/zh-cn/web/javascript/reference/statements/for-await...of/index.html index 47277b5f61..247677e1b9 100644 --- a/files/zh-cn/web/javascript/reference/statements/for-await...of/index.html +++ b/files/zh-cn/web/javascript/reference/statements/for-await...of/index.html @@ -16,7 +16,7 @@ translation_of: Web/JavaScript/Reference/Statements/for-await...of ---{{jsSidebar("Statements")}}
-+
for await...of
语句创建一个循环,该循环遍历异步可迭代对象以及同步可迭代对象,包括: 内置的 {{jsxref("String")}}, {{jsxref("Array")}},类似数组对象 (例如 {{jsxref("Functions/arguments", "arguments")}} 或 {{DOMxRef("NodeList")}}),{{jsxref("TypedArray")}}, {{jsxref("Map")}}, {{jsxref("Set")}} 和用户定义的异步/同步迭代器。它使用对象的每个不同属性的值调用要执行的语句来调用自定义迭代钩子。
for await...of
语句创建一个循环,该循环遍历异步可迭代对象以及同步可迭代对象,包括: 内置的 {{jsxref("String")}}, {{jsxref("Array")}},类似数组对象 (例如 {{jsxref("Functions/arguments", "arguments")}} 或 {{DOMxRef("NodeList")}}),{{jsxref("TypedArray")}}, {{jsxref("Map")}}, {{jsxref("Set")}} 和用户定义的异步/同步迭代器。它使用对象的每个不同属性的值调用要执行的语句来调用自定义迭代钩子。类似于 {{jsxref("Operators/await", "await")}} 运算符一样,该语句只能在一个{{jsxref("Statements/async_function", "async function", "异步函数", 1)}} 内部使用。
diff --git a/files/zh-cn/web/javascript/reference/statements/for...in/index.html b/files/zh-cn/web/javascript/reference/statements/for...in/index.html index 2e4318ba9f..7254cda762 100644 --- a/files/zh-cn/web/javascript/reference/statements/for...in/index.html +++ b/files/zh-cn/web/javascript/reference/statements/for...in/index.html @@ -133,9 +133,9 @@ for (var i = 0 in obj) { // 3
这个非标准行为现在在版本40及更高版本中被忽略,并将在严格模式({{bug(748550)}} 和 {{bug(1164741)}})中呈现{{jsxref("SyntaxError")}}("for-in loop head declarations may not have initializers")错误。
+这个非标准行为现在在版本40及更高版本中被忽略,并将在严格模式({{bug(748550)}} 和 {{bug(1164741)}})中呈现{{jsxref("SyntaxError")}}("for-in loop head declarations may not have initializers")错误。
-像其他引擎 V8(Chrome),Chakra (IE/Edge), JSC (WebKit/Safari) 正在研究去除这种不标准的行为。
+像其他引擎 V8(Chrome),Chakra (IE/Edge), JSC (WebKit/Safari) 正在研究去除这种不标准的行为。
你还可以迭代一个生成器:
+你还可以迭代一个生成器:
function* fibonacci() { // 一个生成器函数 let [prev, curr] = [0, 1]; diff --git a/files/zh-cn/web/javascript/reference/statements/function/index.html b/files/zh-cn/web/javascript/reference/statements/function/index.html index 98d1073a9d..dc052a53d0 100644 --- a/files/zh-cn/web/javascript/reference/statements/function/index.html +++ b/files/zh-cn/web/javascript/reference/statements/function/index.html @@ -19,7 +19,7 @@ translation_of: Web/JavaScript/Reference/Statements/functionThe source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.-语法
+语法
function name([param,[, param,[..., param]]]) { [statements] @@ -98,31 +98,23 @@ if (true) {+JavaScript 中的函数声明被提升到了函数定义。你可以在函数声明之前使用该函数:
-hoisted(); // "foo" +
+function hoisted() { + console.log('foo'); +}function hoisted() { - console.log("foo"); -} - -/* equal to*/ -var hoisted; - -hoisted = function() { - console.log("foo"); -} -hoisted(); -// "foo"hoisted(); // logs "foo" -
-注意 :函数表达式{{jsxref("Operators/function", "function expressions")}} 不会被提升:
+notHoisted(); // TypeError: notHoisted is not a function - -var notHoisted = function() { - console.log("bar"); -};
notHoisted(); // TypeError: notHoisted is not a function + +var notHoisted = function() { + console.log('bar'); +}; +
示例
@@ -130,9 +122,9 @@ var hoistedfunction calc_sales(units_a, units_b, units_c) { - return units_a*79 + units_b * 129 + units_c * 699; -}
function calc_sales(units_a, units_b, units_c) {functionfunctionfunctionfunctionfunctionfunctionfunctionfunctionfunctionfunctionfunction + return units_a*79 + units_b * 129 + units_c * 699; +}
规范
diff --git a/files/zh-cn/web/javascript/reference/statements/function_star_/index.html b/files/zh-cn/web/javascript/reference/statements/function_star_/index.html index 4e1f69883a..90794ec0d4 100644 --- a/files/zh-cn/web/javascript/reference/statements/function_star_/index.html +++ b/files/zh-cn/web/javascript/reference/statements/function_star_/index.html @@ -217,7 +217,7 @@ arr = [...gen]; // ["a", "b", "c", "d", "e"]
IteratorResult
不再抛出错误从Gecko 29 {{geckoRelease(29)}}开始,完成的生成器函数不再抛出{{jsxref("TypeError")}} "generator has already finished". 而是返回一个IteratorResult
对象:{ value: undefined, done: true } ({{bug(958951)}})。
从Gecko 29 {{geckoRelease(29)}}开始,完成的生成器函数不再抛出{{jsxref("TypeError")}} "generator has already finished". 而是返回一个IteratorResult
对象:{ value: undefined, done: true } ({{bug(958951)}})。
当指定条件为真,if 语句会执行一段语句。如果条件为假,则执行另一段语句。
+当指定条件为真,if 语句会执行一段语句。如果条件为假,则执行另一段语句。
{{EmbedInteractiveExample("pages/js/statement-ifelse.html")}}
@@ -38,7 +38,7 @@ translation_of: Web/JavaScript/Reference/Statements/if...elsestatement2
condition
为假且 else
从句存在时执行的语句。可为任意语句,包括块语句和嵌套的if
语句。condition
为假且 else
从句存在时执行的语句。可为任意语句,包括块语句和嵌套的if
语句。引入模块可能有一个default
{{jsxref("Statements/export", "export")}}(无论它是对象,函数,类等)可用。然后可以使用import
语句来导入这样的默认接口。
引入模块可能有一个default
{{jsxref("Statements/export", "export")}}(无论它是对象,函数,类等)可用。然后可以使用import
语句来导入这样的默认接口。
最简单的用法是直接导入默认值:
diff --git a/files/zh-cn/web/javascript/reference/statements/index.html b/files/zh-cn/web/javascript/reference/statements/index.html index c6d70fce55..570cb1adff 100644 --- a/files/zh-cn/web/javascript/reference/statements/index.html +++ b/files/zh-cn/web/javascript/reference/statements/index.html @@ -16,7 +16,7 @@ translation_of: Web/JavaScript/Reference/Statements若需要按字母顺序排列的列表,请参阅左侧边栏。
-下面用符合 ECMAscript 规范的简单的 JavaScript 来编写相同的“条件catch子句”(显然更加冗长的,但是可以在任何地方运行):
+下面用符合 ECMAscript 规范的简单的 JavaScript 来编写相同的“条件catch子句”(显然更加冗长的,但是可以在任何地方运行):
try { myRoutine(); -- cgit v1.2.3-54-g00ecf