blob: b56586ac1ad728ee2772f9c0712b34b59f099cab (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
---
title: Array.prototype.toString()
slug: Web/JavaScript/Reference/Global_Objects/Array/toString
tags:
- Array
- JavaScript
- Method
- Prototype
browser-compat: javascript.builtins.Array.toString
---
<div>{{JSRef}}</div>
<p><code><strong>toString()</strong></code> 方法將回傳一個可以表達該陣列及其元素的字串。 </p>
<div>{{EmbedInteractiveExample("pages/js/array-tostring.html","shorter")}}</div>
<h2 id="Syntax">語法</h2>
<pre class="brush: js">toString()</pre>
<h3 id="Return_value">回傳值</h3>
<p>一個表達該陣列及該陣列中元素的字串。</p>
<h2 id="Description">描述</h2>
<p>
{{jsxref("Array")}} 覆寫了 {{jsxref("Object")}} 中的 <code>toString</code> 方法。
陣列的 <code>toString</code> 方法會將陣列中的每個元素用逗號串接起來成為一個字串,並回傳該字串。
</p>
<p>
當你在會以文字型態表示的地方使用了陣列,或是在字串的串接中使用到了陣列,JavaScript 會自動為該陣列使用<code>toString</code> 方法。
</p>
<h3 id="ECMAScript_5_semantics">ECMAScript 5 語義</h3>
<p>
始於 JavaScript 1.8.5 (Firefox 4),並且和 ECMAScript 5 的語義一致。
<code>toString()</code> 方法是通用的,任何的物件都可以使用。 {{jsxref("Object.prototype.toString()")}} 會被呼叫,並回傳結果。
</p>
<h2 id="Examples">範例</h2>
<h3 id="Using_toString">如何使用 toString</h3>
<pre class="brush: js">const array1 = [1, 2, 'a', '1a'];
console.log(array1.toString());
// expected output: "1,2,a,1a"
</pre>
<h2 id="Specifications">規格</h2>
{{Specifications}}
<h2 id="Browser_compatibility">瀏覽器相容性</h2>
<p>{{Compat}}</p>
<h2 id="See_also">參見</h2>
<ul>
<li>{{jsxref("Array.prototype.join()")}}</li>
<li>{{jsxref("Object.prototype.toSource()")}}</li>
</ul>
|