diff options
Diffstat (limited to 'files/zh-tw/web/javascript/reference/global_objects/string/concat')
-rw-r--r-- | files/zh-tw/web/javascript/reference/global_objects/string/concat/index.html | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/files/zh-tw/web/javascript/reference/global_objects/string/concat/index.html b/files/zh-tw/web/javascript/reference/global_objects/string/concat/index.html new file mode 100644 index 0000000000..5a7e570860 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/string/concat/index.html @@ -0,0 +1,89 @@ +--- +title: String.prototype.concat() +slug: Web/JavaScript/Reference/Global_Objects/String/concat +tags: +- JavaScript +- Method +- Prototype +- Reference +- String +browser-compat: javascript.builtins.String.concat +--- +<div>{{JSRef}}</div> + +<p><span class="seoSummary"><strong><code>concat()</code></strong> 會將呼叫此方法的字串以及作為參數傳遞進此方法的字串串接在一起,並將串接結果作為一個新的字串回傳。</span></p> + +<div>{{EmbedInteractiveExample("pages/js/string-concat.html")}}</div> + + +<h2 id="Syntax">語法</h2> + +<pre class="brush: js"> +concat(str1) +concat(str1, str2) +concat(str1, str2, ... , strN)</pre> + +<h3 id="Parameters">參數</h3> + +<dl> + <dt><code>strN</code></dt> + <dd>要串接到 <code>str</code> 的字串,可以傳入一個至多個。</dd> +</dl> + +<h3 id="Return_value">回傳值</h3> + +<p>此方法會回傳一個新的字串,由呼叫此方法的字串及作為參數傳入的字串組合而成。</p> + +<h2 id="Description">描述</h2> + +<p> + <code>concat()</code> 會將那些作為參數的字串串接在呼叫此方法的字串後面,並作為一個新的字串回傳。 + 對於原先的字串、或是回傳的字串做修改,不會讓他們的值互相影響。 +</p> + +<p> + 如果傳入的參數不是字串型別,那在串接前會先將該參數轉換成字串。 +</p> + +<h2 id="Performance">效能</h2> + +<p> + 對於字串的串接,強烈建議直接使用運算子 {{jsxref("Operators/Assignment_Operators", "assignment operators", "", 1)}} 來達成, + 像是 <code>+</code> 及 <code>+=</code>,而不是使用 <code>concat()</code> 方法。 +</p> + +<h2 id="Examples">範例</h2> + +<h3 id="Using_concat">如何使用 concat()</h3> + +<p>以下的例子示範如何將那些給定的字串組合成新的字串。</p> + +<pre class="brush: js">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="Specifications">規格</h2> + +{{Specifications}} + +<h2 id="Browser_compatibility">瀏覽器相容性</h2> + +<p>{{Compat}}</p> + +<h2 id="See_also">參見</h2> + +<ul> + <li>{{jsxref("Array.prototype.concat()")}}</li> + <li>{{jsxref("Operators/Assignment_Operators", "Assignment operators", "", 1)}}</li> +</ul> |