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/global_objects/array/join | |
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/global_objects/array/join')
-rw-r--r-- | files/zh-tw/web/javascript/reference/global_objects/array/join/index.html | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/files/zh-tw/web/javascript/reference/global_objects/array/join/index.html b/files/zh-tw/web/javascript/reference/global_objects/array/join/index.html new file mode 100644 index 0000000000..0beaecebdd --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/array/join/index.html @@ -0,0 +1,109 @@ +--- +title: Array.prototype.join() +slug: Web/JavaScript/Reference/Global_Objects/Array/join +tags: + - Array + - JavaScript + - Method + - Prototype + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Array/join +--- +<div>{{JSRef}}</div> + +<p><code><strong>join()</strong></code> 方法會將陣列(或一個<a href="/zh-TW/docs/Web/JavaScript/Guide/Indexed_collections#Working_with_array-like_objects">類陣列(array-like)物件</a>)中所有的元素連接、合併成一個字串,並回傳此字串。</p> + +<div>{{EmbedInteractiveExample("pages/js/array-join.html")}}</div> + + + +<h2 id="語法">語法</h2> + +<pre class="syntaxbox"><var>arr</var>.join(<em>[</em><var>separator]</var>)</pre> + +<h3 id="參數">參數</h3> + +<dl> + <dt><code>separator</code> {{optional_inline}}</dt> + <dd>用來隔開陣列中每個元素的字串。如果必要的話,separator 會自動被轉成字串型態。如果未傳入此參數,陣列中的元素將預設用英文逗號(「,」)隔開。如果 <code>separator</code> 是空字串,合併後,元素間不會有任何字元。</dd> +</dl> + +<h3 id="回傳值">回傳值</h3> + +<p>一個合併所有陣列元素的字串。假如 <code><em>arr</em>.length</code> 為 <code>0</code>,將回傳空字串。</p> + +<h2 id="描述">描述</h2> + +<p>將所有陣列中的元素轉成字串型態後,連接合併成一個字串。任何 <code>undefined</code> 或 <code>null</code> 的元素都會被視為空字串處理。</p> + +<h2 id="範例">範例</h2> + +<h3 id="舉例四種合併用法">舉例四種合併用法</h3> + +<p>下方的範例中,首先宣告一個陣列—<code>a</code>,其中有三個元素。接著分別用:預設值、逗號、加號和空字串將陣列連接。</p> + +<pre class="brush: js">var a = ['Wind', 'Rain', 'Fire']; +a.join(); // 'Wind,Rain,Fire' +a.join(', '); // 'Wind, Rain, Fire' +a.join(' + '); // 'Wind + Rain + Fire' +a.join(''); // 'WindRainFire'</pre> + +<h3 id="合併一個類陣列(array-like)物件">合併一個類陣列(array-like)物件</h3> + +<p>下方的範例將合併一個類陣列(array-like)物件(<code><a href="/zh-TW/docs/Web/JavaScript/Reference/Functions/arguments">arguments</a></code>),藉由 {{jsxref("Function.prototype.call")}} 來呼叫 <code>Array.prototype.join</code>。</p> + +<pre class="brush: js">function f(a, b, c) { + var s = Array.prototype.join.call(arguments); + console.log(s); // '<span class="message-body-wrapper"><span class="message-flex-body"><span class="devtools-monospace message-body"><span class="objectBox objectBox-string">1,a,true'</span></span></span></span> +} +f(1, 'a', true); +//expected output: "1,a,true" +</pre> + +<h2 id="規範">規範</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">規範</th> + <th scope="col">狀態</th> + <th scope="col">註解</th> + </tr> + <tr> + <td>{{SpecName('ES1')}}</td> + <td>{{Spec2('ES1')}}</td> + <td>Initial definition. Implemented in JavaScript 1.1.</td> + </tr> + <tr> + <td>{{SpecName('ES5.1', '#sec-15.4.4.5', 'Array.prototype.join')}}</td> + <td>{{Spec2('ES5.1')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ES6', '#sec-array.prototype.join', 'Array.prototype.join')}}</td> + <td>{{Spec2('ES6')}}</td> + <td> </td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-array.prototype.join', 'Array.prototype.join')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="瀏覽器相容性">瀏覽器相容性</h2> + +<div> + + +<p>{{Compat("javascript.builtins.Array.join")}}</p> +</div> + +<h2 id="參見">參見</h2> + +<ul> + <li>{{jsxref("String.prototype.split()")}}</li> + <li>{{jsxref("Array.prototype.toString()")}}</li> + <li>{{jsxref("TypedArray.prototype.join()")}}</li> +</ul> |