From 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:43:23 -0500 Subject: initial commit --- .../reference/global_objects/array/join/index.html | 109 +++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 files/zh-tw/web/javascript/reference/global_objects/array/join/index.html (limited to 'files/zh-tw/web/javascript/reference/global_objects/array/join') 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 +--- +
{{JSRef}}
+ +

join() 方法會將陣列(或一個類陣列(array-like)物件)中所有的元素連接、合併成一個字串,並回傳此字串。

+ +
{{EmbedInteractiveExample("pages/js/array-join.html")}}
+ + + +

語法

+ +
arr.join([separator])
+ +

參數

+ +
+
separator {{optional_inline}}
+
用來隔開陣列中每個元素的字串。如果必要的話,separator 會自動被轉成字串型態。如果未傳入此參數,陣列中的元素將預設用英文逗號(「,」)隔開。如果 separator 是空字串,合併後,元素間不會有任何字元。
+
+ +

回傳值

+ +

一個合併所有陣列元素的字串。假如 arr.length0,將回傳空字串。

+ +

描述

+ +

將所有陣列中的元素轉成字串型態後,連接合併成一個字串。任何 undefinednull 的元素都會被視為空字串處理。

+ +

範例

+ +

舉例四種合併用法

+ +

下方的範例中,首先宣告一個陣列—a,其中有三個元素。接著分別用:預設值、逗號、加號和空字串將陣列連接。

+ +
var a = ['Wind', 'Rain', 'Fire'];
+a.join();      // 'Wind,Rain,Fire'
+a.join(', ');  // 'Wind, Rain, Fire'
+a.join(' + '); // 'Wind + Rain + Fire'
+a.join('');    // 'WindRainFire'
+ +

合併一個類陣列(array-like)物件

+ +

下方的範例將合併一個類陣列(array-like)物件(arguments),藉由 {{jsxref("Function.prototype.call")}} 來呼叫 Array.prototype.join

+ +
function f(a, b, c) {
+  var s = Array.prototype.join.call(arguments);
+  console.log(s); // '1,a,true'
+}
+f(1, 'a', true);
+//expected output: "1,a,true"
+
+ +

規範

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
規範狀態註解
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition. Implemented in JavaScript 1.1.
{{SpecName('ES5.1', '#sec-15.4.4.5', 'Array.prototype.join')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-array.prototype.join', 'Array.prototype.join')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-array.prototype.join', 'Array.prototype.join')}}{{Spec2('ESDraft')}} 
+ +

瀏覽器相容性

+ +
+ + +

{{Compat("javascript.builtins.Array.join")}}

+
+ +

參見

+ + -- cgit v1.2.3-54-g00ecf