diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:43:23 -0500 |
commit | 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 (patch) | |
tree | a9ef8ac1e1b8fe4207b6d64d3841bfb8990b6fd0 /files/zh-tw/web/javascript/reference/statements/block | |
parent | 074785cea106179cb3305637055ab0a009ca74f2 (diff) | |
download | translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.gz translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.tar.bz2 translated-content-218934fa2ed1c702a6d3923d2aa2cc6b43c48684.zip |
initial commit
Diffstat (limited to 'files/zh-tw/web/javascript/reference/statements/block')
-rw-r--r-- | files/zh-tw/web/javascript/reference/statements/block/index.html | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/files/zh-tw/web/javascript/reference/statements/block/index.html b/files/zh-tw/web/javascript/reference/statements/block/index.html new file mode 100644 index 0000000000..62a09df015 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/statements/block/index.html @@ -0,0 +1,86 @@ +--- +title: 區塊 +slug: Web/JavaScript/Reference/Statements/block +translation_of: Web/JavaScript/Reference/Statements/block +--- +<p>{{jsSidebar("Statements")}}</p> + +<h2 id="總覽">總覽</h2> + +<p>區塊陳述用來組合零個或多個陳述。我們使用一對大括號 { } 以界定區塊。</p> + +<table class="standard-table"> + <tbody> + <tr> + <td class="header" colspan="2">陳述句</td> + </tr> + <tr> + <td>Implemented in</td> + <td>JavaScript 1.0</td> + </tr> + <tr> + <td>ECMAScript edition</td> + <td>ECMA-262 1st edition</td> + </tr> + </tbody> +</table> + +<h2 id="語法">語法</h2> + +<pre class="syntaxbox">{ + <var>陳述_1</var> + <var>陳述_2</var> + ... + <var>陳述_n</var> +} +</pre> + +<h3 id="參數">參數</h3> + +<dl> + <dt><code>陳述_1</code>, <code>陳述_2</code>, <code>陳述_n</code></dt> + <dd>區塊陳述中的陳述句群。</dd> +</dl> + +<h2 id="說明">說明</h2> + +<p>區塊陳述通常配合流程控制陳述(如 <code>if</code>、<code>for</code>、<code>while</code>)一併使用。</p> + +<h4 id="var"><code>var</code></h4> + +<p>使用<code>var</code>區塊中定義的變數,其存取範圍是整個整個函式或是腳本,即為Execution Context的範圍中。</p> + +<pre class="brush: js">var x = 1; +{ + var x = 2; +} +alert(x); // outputs 2 +</pre> + +<p>輸出結果是 2。因為var是宣告於整個腳本範圍中。</p> + +<h4 id="let_和_const"><code>let </code>和 <code>const</code></h4> + +<p>當使用<code>let</code>或是<code>const</code>進行宣告時,其存取範圍是只有本身定義的區塊中。</p> + +<pre class="brush: js line-numbers language-js"><code class="language-js"><span class="keyword token">let</span> x <span class="operator token">=</span> <span class="number token">1</span><span class="punctuation token">;</span> +<span class="punctuation token">{</span> + <span class="keyword token">let</span> x <span class="operator token">=</span> <span class="number token">2</span><span class="punctuation token">;</span> +<span class="punctuation token">}</span> +console<span class="punctuation token">.</span><span class="function token">log</span><span class="punctuation token">(</span>x<span class="punctuation token">)</span><span class="punctuation token">;</span> <span class="comment token">// logs 1</span></code></pre> + +<h4 id="function"><code>function</code></h4> + +<p>當function被呼叫時,會建立此function的Execution Context,因此在function區塊使用<code>var</code>整個function區塊中都可對其進行存取。</p> + +<pre class="brush: js">function foo() { + { + var a = 'var'; + { + let a = 'let'; + console.log(a); // let + } + } + console.log(a); // var +} +foo();</pre> |