diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/array/join | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/array/join')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/array/join/index.html | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/array/join/index.html b/files/zh-cn/web/javascript/reference/global_objects/array/join/index.html new file mode 100644 index 0000000000..c40b87ebed --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/array/join/index.html @@ -0,0 +1,111 @@ +--- +title: Array.prototype.join() +slug: Web/JavaScript/Reference/Global_Objects/Array/join +tags: + - Array + - Array.prototype.join() + - join() + - separator +translation_of: Web/JavaScript/Reference/Global_Objects/Array/join +--- +<div>{{JSRef}}</div> + +<p><code><strong>join()</strong></code> 方法将一个数组(或一个<a href="/zh-CN//docs/Web/JavaScript/Guide/Indexed_collections#Working_with_array-like_objects">类数组对象</a>)的所有元素连接成一个字符串并返回这个字符串。如果数组只有一个项目,那么将返回该项目而不使用分隔符。</p> + +<div>{{EmbedInteractiveExample("pages/js/array-join.html")}}</div> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre><var>arr</var>.join([<var>separator</var>])</pre> + +<h3 id="Parameters" name="Parameters">参数</h3> + +<dl> + <dt><code>separator</code> {{optional_inline}}</dt> + <dd>指定一个字符串来分隔数组的每个元素。如果需要,将分隔符转换为字符串。如果缺省该值,数组元素用逗号(<code>,</code>)分隔。如果<code>separator</code>是空字符串(<code>""</code>),则所有元素之间都没有任何字符。</dd> + <dt> + <h3 id="返回值">返回值</h3> + </dt> + <dd>一个所有数组元素连接的字符串。如果 <code>arr.length</code><strong> </strong>为0,则返回空字符串。</dd> +</dl> + +<h2 id="Description" name="Description">描述</h2> + +<p>所有的数组元素被转换成字符串,再用一个分隔符将这些字符串连接起来。</p> + +<div class="blockIndicator warning"> +<p>如果一个元素为 <code>undefined</code> 或 <code>null</code>,它会被转换为空字符串。</p> +</div> + +<h2 id="Examples" name="Examples">示例</h2> + +<h3 id="Example_Joining_an_array_three_different_ways" name="Example:_Joining_an_array_three_different_ways">使用四种不同的分隔符连接数组元素</h3> + +<p>下例首先创建了一个数组 <code>a</code>,包含有三个元素,然后用四种不同的分隔符连接所有数组元素。首先是默认的分隔符逗号,然后是一个逗号加空格,接下来是一个加号前后加空格,最后是一个空字符串。</p> + +<pre class="brush: js">var a = ['Wind', 'Rain', 'Fire']; +var myVar1 = a.join(); // myVar1的值变为"Wind,Rain,Fire" +var myVar2 = a.join(', '); // myVar2的值变为"Wind, Rain, Fire" +var myVar3 = a.join(' + '); // myVar3的值变为"Wind + Rain + Fire" +var myVar4 = a.join(''); // myVar4的值变为"WindRainFire" +</pre> + +<h3 id="连接类数组对象">连接类数组对象</h3> + +<p>下面的示例将连接类数组对象(arguments),通过在<code>Array.prototype.join</code>上调用{{jsxref("Function.prototype.call")}}。</p> + +<pre class="brush: js"><code>function f(a, b, c) { + var s = Array.prototype.join.call(arguments); + console.log(s); // '1,a,true' +} +f(1, 'a', true);</code></pre> + +<h2 id="Specifications" name="Specifications">规范</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.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="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<div> +<div> + + +<p>{{Compat("javascript.builtins.Array.join")}}</p> +</div> +</div> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li>{{jsxref("String.prototype.split()")}}</li> + <li>{{jsxref("Array.prototype.toString()")}}</li> + <li>{{jsxref("TypedArray.prototype.join()")}}</li> +</ul> |