From c0e0c18dad0c085a90296f70274a9b3cc0aea9e3 Mon Sep 17 00:00:00 2001 From: Chris Huang Date: Sat, 8 May 2021 21:10:07 +0800 Subject: Update /web/javascript/reference/global_objects/string, zh-TW (#792) * Update /web/javascript/reference/global_objects/string/concat/, zh-TW * Update /web/javascript/reference/global_objects/string/trim/, zh-TW * Update /web/javascript/reference/global_objects/string/concat, zh-TW * Update /web/javascript/reference/global_objects/string/trim, zh-TW Co-authored-by: chrishuang --- .../global_objects/string/concat/index.html | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 files/zh-tw/web/javascript/reference/global_objects/string/concat/index.html (limited to 'files/zh-tw/web/javascript/reference/global_objects/string/concat/index.html') 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 +--- +
{{JSRef}}
+ +

concat() 會將呼叫此方法的字串以及作為參數傳遞進此方法的字串串接在一起,並將串接結果作為一個新的字串回傳。

+ +
{{EmbedInteractiveExample("pages/js/string-concat.html")}}
+ + +

語法

+ +
+concat(str1)
+concat(str1, str2)
+concat(str1, str2, ... , strN)
+ +

參數

+ +
+
strN
+
要串接到 str 的字串,可以傳入一個至多個。
+
+ +

回傳值

+ +

此方法會回傳一個新的字串,由呼叫此方法的字串及作為參數傳入的字串組合而成。

+ +

描述

+ +

+ concat() 會將那些作為參數的字串串接在呼叫此方法的字串後面,並作為一個新的字串回傳。 + 對於原先的字串、或是回傳的字串做修改,不會讓他們的值互相影響。 +

+ +

+ 如果傳入的參數不是字串型別,那在串接前會先將該參數轉換成字串。 +

+ +

效能

+ +

+ 對於字串的串接,強烈建議直接使用運算子 {{jsxref("Operators/Assignment_Operators", "assignment operators", "", 1)}} 來達成, + 像是 ++=,而不是使用 concat() 方法。 +

+ +

範例

+ +

如何使用 concat()

+ +

以下的例子示範如何將那些給定的字串組合成新的字串。

+ +
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"
+
+
+ +

規格

+ +{{Specifications}} + +

瀏覽器相容性

+ +

{{Compat}}

+ +

參見

+ + -- cgit v1.2.3-54-g00ecf