JS裏並無標準的多行字符串的表示方法,可是在用模板的時候,爲了保證模板的可閱讀性,咱們又不可避免的使用多行字符串,因此出現了各類搞法,這裏以一段jade的模板做爲示例,簡單總結和對比一下。javascript
這是最容易理解也很經常使用的一種形式,以下php
var tmpl =''+ '!!! 5' + 'html' + ' include header' + ' body' + ' //if IE 6' + ' .alert.alert-error' + ' center 對不起,咱們不支持IE6,請升級你的瀏覽器' + ' a(href="http://windows.microsoft.com/zh-CN/internet-explorer/download-ie") | IE8官方下載' + ' a(href="https://www.google.com/intl/en/chrome/browser/") | Chrome下載' + ' include head' + ' .container' + ' .row-fluid' + ' .span8' + ' block main' + ' include pagerbar' + ' .span4' + ' include sidebar' + ' include footer' + ' include script'
優勢:html
缺點 :java
\n
+
號看上去滿天星,大量的'
和"
, 醜陋這個叫續行符, 這個並不是一種很常見的方式, 可是一旦用上了,仍是很容易上癮,只須要加一個字符git
var tmpl ='\ !!! 5\ html\ include header\ body\ //if IE 6\ .alert.alert-error\ center 對不起,咱們不支持IE6,請升級你的瀏覽器\ a(href="http://windows.microsoft.com/zh-CN/internet-explorer/download-ie") | IE8官方下載\ a(href="https://www.google.com/intl/en/chrome/browser/") | Chrome下載\ include head\ .container\ .row-fluid\ .span8\ block main\ include pagerbar\ .span4\ include sidebar\ include footer\ include script'
優勢:es6
\
缺點 :github
\
必須不能夠有空格,不然直接腳本錯誤\n
var tmpl = [ '!!! 5' , 'html' , ' include header' , ' body' , ' //if IE 6' , ' .alert.alert-error' , ' center 對不起,咱們不支持IE6,請升級你的瀏覽器' , ' a(href="http://windows.microsoft.com/zh-CN/internet-explorer/download-ie") | IE8官方下載' , ' a(href="https://www.google.com/intl/en/chrome/browser/") | Chrome下載' , ' include head' , ' .container' , ' .row-fluid' , ' .span8' , ' block main' , ' include pagerbar' , ' .span4' , ' include sidebar' , ' include footer' , ' include script'].join('\n');
優勢:chrome
缺點 :typescript
,
號和'
、"
, 醜陋var tmpl = String.prototype.concat.call( '!!! 5' , 'html' , ' include header' , ' body' , ' //if IE 6' , ' .alert.alert-error' , ' center 對不起,咱們不支持IE6,請升級你的瀏覽器' , ' a(href="http://windows.microsoft.com/zh-CN/internet-explorer/download-ie") | IE8官方下載' , ' a(href="https://www.google.com/intl/en/chrome/browser/") | Chrome下載' , ' include head' , ' .container' , ' .row-fluid' , ' .span8' , ' block main' , ' include pagerbar' , ' .span4' , ' include sidebar' , ' include footer' , ' include script');
優勢:windows
+
號常見缺點 :
,
號和'
、"
, 醜陋這是一種頗有技巧的解決辦法, 利用了function的toString方法
function heredoc(fn) { return fn.toString().split('\n').slice(1,-1).join('\n') + '\n' } var tmpl = heredoc(function(){/* !!! 5 html include header body //if IE 6 .alert.alert-error center 對不起,咱們不支持IE6,請升級你的瀏覽器 a(href="http://windows.microsoft.com/zh-CN/internet-explorer/download-ie") | IE8官方下載 a(href="https://www.google.com/intl/en/chrome/browser/") | Chrome下載 include head .container .row-fluid .span8 block main include pagerbar .span4 include sidebar include footer include script */});
優勢:
\n
哦缺點 :
至關於換了一個語言,其實這種語言上缺乏的功能,經過coffeescript這種以js爲編譯目標的語言來實現是一種很是棒的選擇。
var tmpl = """ !!! 5 html include header body //if IE 6 .alert.alert-error center 對不起,咱們不支持IE6,請升級你的瀏覽器 a(href="http://windows.microsoft.com/zh-CN/internet-explorer/download-ie") | IE8官方下載 a(href="https://www.google.com/intl/en/chrome/browser/") | Chrome下載 include head .container .row-fluid .span8 block main include pagerbar .span4 include sidebar include footer include script """
優勢:
缺點 :
ES6的有一個新的特性,Template Strings, 這是語言層面上第一次實現了多行字符串, 在chrome canary裏打開Enable Experimental JavaScript
就可使用這個特性,另外typescript也會支持這種方式
var tmpl = `!!! 5 html include header body //if IE 6 .alert.alert-error center 對不起,咱們不支持IE6,請升級你的瀏覽器 a(href="http://windows.microsoft.com/zh-CN/internet-explorer/download-ie") | IE8官方下載 a(href="https://www.google.com/intl/en/chrome/browser/") | Chrome下載 include head .container .row-fluid .span8 block main include pagerbar .span4 include sidebar include footer include script`
優勢:
缺點 :
看了這麼些寫法,如何選擇?若是你用的是coffeescript,放心大膽的使用它支持的多行字符串寫法;若是是在客戶端,同時你解決了你的壓縮器去掉註釋的問題,推薦使用heredoc;若是你沒法解決壓縮器的問題,使用反斜線鏈接吧,每行只須要加一個字符。