aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/array/tostring/index.html
blob: 2378e078f85bc99f087861e1f097b012c10426c5 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
---
title: Array.prototype.toString()
slug: Web/JavaScript/Reference/Global_Objects/Array/toString
tags:
  - Array
  - JavaScript
  - Method
translation_of: Web/JavaScript/Reference/Global_Objects/Array/toString
---
<div>{{JSRef("Global_Objects", "Array")}}</div>

<p><code><strong>toString()</strong></code> 返回一个字符串,表示指定的数组及其元素。</p>

<div>{{EmbedInteractiveExample("pages/js/array-tostring.html")}}</div>

<h2 id="Syntax" name="Syntax">语法</h2>

<pre class="brush: js">arr.toString()
</pre>

<h3 id="Parameters" name="Parameters">返回值</h3>

<p>一个表示指定的数组及其元素的字符串。</p>

<h2 id="描述">描述</h2>

<p>{{jsxref("Array")}}对象覆盖了{{jsxref("Object")}}<code>toString</code> 方法。对于数组对象,<code>toString</code> 方法连接数组并返回一个字符串,其中包含用逗号分隔的每个数组元素。</p>

<p>当一个数组被作为文本值或者进行字符串连接操作时,将会自动调用其 <code>toString</code> 方法。</p>

<h3 id="ECMAScript_5_semantics">ECMAScript 5 semantics</h3>

<p>从 JavaScript 1.8.5 (Firefox 4) 开始,和 ECMAScript 第5版语义(semantics)一致,<code>toString()</code> 方法是通用的,可被用于任何对象。将调用{{jsxref("Object.prototype.toString()")}},并返回结果值。</p>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</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.2', 'Array.prototype.toString')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-array.prototype.tostring', 'Array.prototype.toString')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-array.prototype.tostring', 'Array.prototype.toString')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>

<div>


<p>{{Compat("javascript.builtins.Array.toString")}}</p>
</div>

<h2 id="See_Also" name="See_Also">相关链接</h2>

<ul>
 <li>{{jsxref("Array.prototype.join()")}}</li>
 <li>{{jsxref("Object.prototype.toSource()")}}</li>
</ul>