aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/string/concat/index.html
blob: ee2d8dd06d0412f514d07f7cec36d6a5c1dc6957 (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
81
82
83
---
title: String.prototype.concat()
slug: Web/JavaScript/Reference/Global_Objects/String/concat
tags:
  - JavaScript
  - Method
  - Prototype
  - String
translation_of: Web/JavaScript/Reference/Global_Objects/String/concat
---
<div>{{JSRef}}</div>

<p><strong><code>concat()</code></strong> 方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。</p>

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

<pre class="notranslate"><code><var>str</var>.concat(<var>str2</var>, [, ...<var>strN</var>])</code></pre>

<h3 id="Parameters" name="Parameters">参数</h3>

<dl>
 <dt><code><var>str2</var> [, ...<var>strN</var>]</code></dt>
 <dd>需要连接到 <code><var>str</var></code> 的字符串。</dd>
</dl>

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

<p>一个新的字符串,包含参数所提供的连接字符串。</p>

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

<p><code>concat</code> 方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。 <code>concat</code> 方法并不影响原字符串。</p>

<p>如果参数不是字符串类型,它们在连接之前将会被转换成字符串。</p>

<h2 id="性能" style="margin-bottom: 20px; line-height: 30px;">性能</h2>

<p>强烈建议使用<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Assignment_Operators">赋值操作符</a><code>+</code>, <code>+=</code>)代替 <code>concat</code> 方法。</p>

<h2 id="Examples" name="Examples">示例</h2>

<h3 id="Examples" name="Examples">使用 <code>concat</code></h3>

<p>下面的例子演示如何将多个字符串与原字符串合并为一个新字符串</p>

<pre class="brush: js notranslate">let hello = 'Hello, '
console.log(hello.concat('Kevin', '. Have a nice day.'))
// Hello, Kevin. Have a nice day.

let greetList = ['Hello', ' ', 'Venkat', '!']
"".concat(...greetList)  // "Hello Venkat!"

"".concat({})    // [object Object]
"".concat([])    // ""
"".concat(null)  // "null"
"".concat(true)  // "true"
"".concat(4, 5)  // "45"</pre>

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

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-string.prototype.concat', 'String.prototype.concat')}}</td>
  </tr>
 </tbody>
</table>

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

<p>{{Compat("javascript.builtins.String.concat")}}</p>

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

<ul>
 <li>{{jsxref("Array.prototype.concat()")}}</li>
 <li>{{jsxref("Operators/Assignment_Operators", "Assignment operators", "", 1)}}</li>
</ul>