在php、python中都有heredoc方式的字串定義方法: php: 複製程式碼 程式碼如下: $sql= select * from pages where pagename='$pn' EOD; python: 複製程式碼 程式碼如下: print """ This is an example of a string in the heredoc syntax. This text can span multiple lines """ js拼接大量字串沒個heredoc風格的操作符是比較繁瑣的: 拼接方式一: 複製程式碼 程式碼如下: var str = " Here is line one And line two Finally, line three! "; alert(str); 拼接方式二: 複製程式碼 程式碼如下: var __template = '' '#salarySN#' '#name#' '#TDR_NAME#' '#TSD_NAME#' '#WORK_STATUS#' '#isleader_display#' '' '設定角色' ' '; JS字串需要打破原字串風格,每行處理,這點有點讓人受不了。 給個解: 複製程式碼 程式碼如下: function aHereDoc() {/* Hello, World! I am a JavaScript here document. Use the 'hereDoc' function to extract me. */} function hereDoc(func) { return func.toString().split(/n/).slice(1, -1).join('n'); } console.log(hereDoc(aHereDoc)); 利用func.toString()取得需要批次處理的字串,利用split(/n/).slice(1, -1)去掉首尾兩行函數定義的程式碼,重新組裝即可。