javascript的創建多行字符串的除了平時常見的”“+ 反斜杠\ 還有ES6的` `
在js51上發現一個很有意思的方法
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哦
缺點 :
不可以在單個字符串中添加js邏輯
容易被壓縮器壓縮掉,yui compressor可以通過/*!來避免被壓縮掉,uglifyjs和gcc也可以通過選項配置不刪除特定的注釋,這個不是大問題