diff options
Diffstat (limited to 'files/zh-tw/web/javascript/reference/statements')
-rw-r--r-- | files/zh-tw/web/javascript/reference/statements/if...else/index.html | 18 | ||||
-rw-r--r-- | files/zh-tw/web/javascript/reference/statements/let/index.html | 26 |
2 files changed, 22 insertions, 22 deletions
diff --git a/files/zh-tw/web/javascript/reference/statements/if...else/index.html b/files/zh-tw/web/javascript/reference/statements/if...else/index.html index a9317aa8a6..9224af1894 100644 --- a/files/zh-tw/web/javascript/reference/statements/if...else/index.html +++ b/files/zh-tw/web/javascript/reference/statements/if...else/index.html @@ -13,7 +13,7 @@ translation_of: Web/JavaScript/Reference/Statements/if...else <h2 id="語法">語法</h2> -<pre class="syntaxbox notranslate">if (<em>條件式</em>) +<pre class="syntaxbox">if (<em>條件式</em>) <em>statement1</em> [else <em>statement2</em>] @@ -39,7 +39,7 @@ translation_of: Web/JavaScript/Reference/Statements/if...else <p>多重的 <code>if...else</code> 陳述式可以使用 <code>else if</code> 子句來建立一個巢狀結構的句子。要記住,在JavaScript中沒有 <code>elseif</code> (一個單字) 的語法可以用。</p> -<pre class="eval notranslate">if (<em>condition1</em>) +<pre class="eval">if (<em>condition1</em>) <em>statement1</em> else if (<em>condition2</em>) <em>statement2</em> @@ -52,7 +52,7 @@ else <p>將巢狀結構適當的排版後,我們能更了解其背後運作的邏輯:</p> -<pre class="eval notranslate">if (<em>condition1</em>) +<pre class="eval">if (<em>condition1</em>) <em>statement1</em> else if (<em>condition2</em>) @@ -64,7 +64,7 @@ else <p>如果在一個條件式中有多個陳述要執行,可以使用區塊陳述式(<code>{ ... }</code>) 把所有陳述包在一起。 通常來說,無論如何都使用區塊陳述式是個很好的習慣,尤其是當你使用巢狀結構的 <code>if</code> 陳述式時,這會讓人更容易理解你的程式碼。</p> -<pre class="eval notranslate">if (<em>condition</em>) { +<pre class="eval">if (<em>condition</em>) { <em>statements1</em> } else { <em>statements2</em> @@ -73,7 +73,7 @@ else <p>不要被<a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean" title="en/JavaScript/Reference/Global_Objects/Boolean">Boolean</a>物件中,布林值的 <code>true</code> 和 <code>false</code> 給混淆了。任何值只要不是 <code>false</code>、 <code>undefined</code>、 <code>null</code>、 <code>0</code>、 <code>NaN</code>,或者空字串 (<code>""</code>),並且任何物件,包括其值是 <code>false</code>的布林物件 ,仍然會被條件陳述式視為條件成立。舉例而言:</p> -<pre class="brush: js notranslate">var b = new Boolean(false); +<pre class="brush: js">var b = new Boolean(false); if (b) // this condition is truthy </pre> @@ -81,7 +81,7 @@ if (b) // this condition is truthy <h3 id="使用_if...else">使用 <code>if...else</code></h3> -<pre class="brush: js notranslate">if (cipher_char === from_char) { +<pre class="brush: js">if (cipher_char === from_char) { result = result + to_char; x++; } else { @@ -93,7 +93,7 @@ if (b) // this condition is truthy <p>要記得JavaScript沒有 <code>elseif</code> 可以使用。不過,你可以使用 <code>else</code> 和 <code>if</code>中間夾著空白的語法:</p> -<pre class="brush: js notranslate">if (x > 5) { +<pre class="brush: js">if (x > 5) { /* do the right thing */ } else if (x > 50) { /* do the right thing */ @@ -105,14 +105,14 @@ if (b) // this condition is truthy <p>建議不要在條件表達式中直接對物件賦值,因為這會使人在瀏覽程式碼時很容易將賦值( assignment )與相等( equality )混淆。舉例而言,不要使用以下寫法:</p> -<pre class="brush: js example-bad notranslate">if (x = y) { +<pre class="brush: js example-bad">if (x = y) { /* do the right thing */ } </pre> <p>如果你必須在條件表達式中使用賦值,最好ˇ的作法是以額外的括號包住賦值語句,如下所示:</p> -<pre class="brush: js example-good notranslate">if ((x = y)) { +<pre class="brush: js example-good">if ((x = y)) { /* do the right thing */ } </pre> diff --git a/files/zh-tw/web/javascript/reference/statements/let/index.html b/files/zh-tw/web/javascript/reference/statements/let/index.html index 0cdc8806be..f3170bb603 100644 --- a/files/zh-tw/web/javascript/reference/statements/let/index.html +++ b/files/zh-tw/web/javascript/reference/statements/let/index.html @@ -13,7 +13,7 @@ translation_of: Web/JavaScript/Reference/Statements/let <h2 id="語法">語法</h2> -<pre class="syntaxbox notranslate">let var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]];</pre> +<pre class="syntaxbox">let var1 [= value1] [, var2 [= value2]] [, ..., varN [= valueN]];</pre> <h3 id="參數">參數</h3> @@ -32,7 +32,7 @@ translation_of: Web/JavaScript/Reference/Statements/let <p>宣告 <code>let</code> 的作用範圍是它們被定義的區塊,以及該區塊包含的子區塊。這樣看起來功能跟 <strong><code>var</code></strong> 很相似。主要不同的地方在於 <strong><code>var</code></strong> 作用範圍是「整個」function:</p> -<pre class="brush:js notranslate">function varTest() { +<pre class="brush:js">function varTest() { var x = 1; { var x = 2; // 這裡的 x 與 function 區塊內部的 x 是一樣的,因此會影響 function 區塊內所有的 x @@ -52,7 +52,7 @@ function letTest() { <p>在上列例子裡的最前行 <code>let</code> 和 <code>var</code> 不同,<code>let</code> 並不會在全域物件中建立變數。舉例來說:</p> -<pre class="brush:js notranslate">var x = 'global'; +<pre class="brush:js">var x = 'global'; let y = 'global'; console.log(this.x); // "global" console.log(this.y); // undefined @@ -62,7 +62,7 @@ console.log(this.y); // undefined <p>In dealing with <a href="/en-US/docs/Glossary/Constructor">constructors</a> it is possible to use the <strong><code>let</code></strong> bindings to share one or more private members without using <a href="/en-US/docs/Web/JavaScript/Closures">closures</a>:</p> -<pre class="brush:js notranslate">var Thing; +<pre class="brush:js">var Thing; { let privateScope = new WeakMap(); @@ -104,21 +104,21 @@ thing.showPrivate(); <p>Redeclaring the same variable within the same function or block scope raises a {{jsxref("SyntaxError")}}.</p> -<pre class="brush: js example-bad notranslate">if (x) { +<pre class="brush: js example-bad">if (x) { let foo; let foo; // SyntaxError thrown. }</pre> <p>In ECMAScript 2015, <strong><code>let</code></strong> bindings are not subject to <strong>Variable Hoisting</strong>, which means that <strong><code>let</code></strong> declarations do not move to the top of the current execution context. Referencing the variable in the block before the initialization results in a <code><a href="/en-US/docs/JavaScript/Reference/Global_Objects/ReferenceError">ReferenceError</a></code> (contrary to a variable declared with <a href="/en-US/docs/Web/JavaScript/Reference/Statements/var#var_hoisting">var</a>, which will just have the undefined value). The variable is in a "temporal dead zone" from the start of the block until the initialization is processed.</p> -<pre class="brush: js notranslate">function do_something() { +<pre class="brush: js">function do_something() { console.log(foo); // ReferenceError let foo = 2; }</pre> <p>你可能會在 <a href="https://developer.mozilla.org/zh-TW/docs/JavaScript/Reference/Statements/switch" title="switch"><code>switch</code></a> 中遇到錯誤,因為所有的 <code>case</code> 都屬於同樣的區塊中。</p> -<pre class="brush: js notranslate">switch (x) { +<pre class="brush: js">switch (x) { case 0: let foo; break; @@ -132,7 +132,7 @@ thing.showPrivate(); <p>You can use the <code>let</code> keyword to bind variables locally in the scope of <code>for</code> loops. This is different from the var keyword in the head of a for loop, which makes the variables visible in the whole function containing the loop.</p> -<pre class="brush:js notranslate">var i=0; +<pre class="brush:js">var i=0; for ( let i=i ; i < 10 ; i++ ) { console.log(i); } @@ -140,7 +140,7 @@ for ( let i=i ; i < 10 ; i++ ) { <p>However, it's important to point out that a block nested inside a case clause will create a new block scoped lexical environment, which will not produce the redeclaration errors shown above.</p> -<pre class="brush: js notranslate">let x = 1; +<pre class="brush: js">let x = 1; switch(x) { case 0: { @@ -157,7 +157,7 @@ switch(x) { <p>Unlike with simply undeclared variables and variables that hold a value of <code>undefined</code>, using the <code>typeof</code> operator to check for the type of a variable in that variable's TDZ will throw a <code>ReferenceError</code>:</p> -<pre class="brush: js notranslate">// prints out 'undefined' +<pre class="brush: js">// prints out 'undefined' console.log(typeof undeclaredVariable); // results in a 'ReferenceError' console.log(typeof i); @@ -168,7 +168,7 @@ let i = 10;</pre> <p>Due to lexical scoping, the identifier<strong> "foo"</strong> inside the expression <code>(foo + 55)</code> evaluates to the <u>if block's foo</u>, and <strong>not</strong> the <u>overlying variable foo</u> with the value of 33.<br> In that very line, the <u>if block's "foo"</u> has already been created in the lexical environment, but has not yet reached (and <strong>terminated</strong>) its initialization (which is part of the statement itself): it's still in the temporal dead zone.</p> -<pre class="brush: js example-bad notranslate">function test(){ +<pre class="brush: js example-bad">function test(){ var foo = 33; { let foo = (foo + 55); // ReferenceError @@ -178,7 +178,7 @@ test();</pre> <p>This phenomenon may confuse you in a situation like the following. The instruction <code>let n of n.a</code> is already inside the private scope of the <u>for loop's block</u>, hence the identifier<strong> "n.a"</strong> is resolved to the property 'a' of the <u>'n' object located in the first part of the instruction itself</u> ("let n"), which is still in the temporal dead zone since its declaration statement has not been reached and <strong>terminated</strong>.</p> -<pre class="brush: js example-bad notranslate">function go(n) { +<pre class="brush: js example-bad">function go(n) { // n here is defined! console.log(n); // Object {a: [1,2,3]} @@ -194,7 +194,7 @@ go({a: [1, 2, 3]}); <p>When used inside a block, <strong><code>let</code></strong> limits the variable's scope to that block. Note the difference between <code><strong>var</strong></code><em> </em>whose scope is inside the function where it is declared.</p> -<pre class="brush: js notranslate">var a = 1; +<pre class="brush: js">var a = 1; var b = 2; if (a === 1) { |