javascript的創(chuàng)建多行字符串的7種方法
JS里并沒有標(biāo)準(zhǔn)的多行字符串的表示方法,但是在用模板的時(shí)候,為了保證模板的可閱讀性,我們又不可避免的使用多行字符串,所以出現(xiàn)了各種搞法,這里以一段jade的模板作為示例,簡單總結(jié)和對比一下。
一、字符串相加
這是最容易理解也很常用的一種形式,如下
var tmpl =''+
'!!! 5' +
'html' +
' include header' +
' body' +
' //if IE 6' +
' .alert.alert-error' +
' center 對不起,我們不支持IE6,請升級(jí)你的瀏覽器' +
' a() | IE8官方下載' +
' a() | Chrome下載' +
' include head' +
' .container' +
' .row-fluid' +
' .span8' +
' block main' +
' include pagerbar' +
' .span4' +
' include sidebar' +
' include footer' +
' include script'
優(yōu)點(diǎn):
易理解,簡單,可靠
足夠靈活,可以在單個(gè)字符串中添加js邏輯
缺點(diǎn) :
并不是真正意義上的多行字符串, 如果想要真正的多行,需要自己加\n
大量的+號(hào)看上去滿天星,大量的'和", 丑陋
二、使用反斜線
這個(gè)叫續(xù)行符, 這個(gè)并非一種很常見的方式, 但是一旦用上了,還是很容易上癮,只需要加一個(gè)字符
var tmpl ='\
!!! 5\
html\
include header\
body\
//if IE 6\
.alert.alert-error\
center 對不起,我們不支持IE6,請升級(jí)你的瀏覽器\
a() | IE8官方下載\
a() | Chrome下載\
include head\
.container\
.row-fluid\
.span8\
block main\
include pagerbar\
.span4\
include sidebar\
include footer\
include script'
優(yōu)點(diǎn):
簡單,每一行只需要有多一個(gè)\
高效!在大部分的瀏覽器上,這種方式都是最快的,
缺點(diǎn) :
致命缺陷,每一行的\必須不可以有空格,否則直接腳本錯(cuò)誤
并不是真正意義上的多行字符串, 如果想要真正的多行,需要自己加\n
盡管絕大部分的js引擎都支持它,但是它并不是ECMAScript的一部分
三、字符串?dāng)?shù)組join
var tmpl = [
'!!! 5' ,
'html' ,
' include header' ,
' body' ,
' //if IE 6' ,
' .alert.alert-error' ,
' center 對不起,我們不支持IE6,請升級(jí)你的瀏覽器' ,
' a() | IE8官方下載' ,
' a() | Chrome下載' ,
' include head' ,
' .container' ,
' .row-fluid' ,
' .span8' ,
' block main' ,
' include pagerbar' ,
' .span4' ,
' include sidebar' ,
' include footer' ,
' include script'].join('\n');
優(yōu)點(diǎn):
真正意義上的多行字符串
易理解,簡單,可靠
足夠靈活,可以在單個(gè)字符串中添加js邏輯
缺點(diǎn) :
大量的,號(hào)和'、", 丑陋
五、String.prototype.concat
var tmpl = String.prototype.concat.call(
'!!! 5' ,
'html' ,
' include header' ,
' body' ,
' //if IE 6' ,
' .alert.alert-error' ,
' center 對不起,我們不支持IE6,請升級(jí)你的瀏覽器' ,
' a() | IE8官方下載' ,
' a() | Chrome下載' ,
' include head' ,
' .container' ,
' .row-fluid' ,
' .span8' ,
' block main' ,
' include pagerbar' ,
' .span4' ,
' include sidebar' ,
' include footer' ,
' include script');
優(yōu)點(diǎn):
不常用,事實(shí)上字符串的concat方法遠(yuǎn)沒有+號(hào)常見
易理解,簡單,可靠
足夠靈活,可以在單個(gè)字符串中添加js邏輯
缺點(diǎn) :
并不是真正意義上的多行字符串
大量的,號(hào)和'、", 丑陋
五、heredoc
這是一種很有技巧的解決辦法, 利用了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,請升級(jí)你的瀏覽器
a() | IE8官方下載
a() | Chrome下載
include head
.container
.row-fluid
.span8
block main
include pagerbar
.span4
include sidebar
include footer
include script
*/});
優(yōu)點(diǎn):
模板字符串內(nèi)不必寫多余的任何字符,干凈,簡單
真正意義上的多行字符串, 有\(zhòng)n哦
缺點(diǎn) :
不可以在單個(gè)字符串中添加js邏輯
容易被壓縮器壓縮掉,yui compressor可以通過/*!來避免被壓縮掉,uglifyjs和gcc也可以通過選項(xiàng)配置不刪除特定的注釋,這個(gè)不是大問題
六、coffeescript
相當(dāng)于換了一個(gè)語言,其實(shí)這種語言上缺少的功能,通過coffeescript這種以js為編譯目標(biāo)的語言來實(shí)現(xiàn)是一種非常棒的選擇。
var tmpl = """
!!! 5
html
include header
body
//if IE 6
.alert.alert-error
center 對不起,我們不支持IE6,請升級(jí)你的瀏覽器
a() | IE8官方下載
a() | Chrome下載
include head
.container
.row-fluid
.span8
block main
include pagerbar
.span4
include sidebar
include footer
include script
"""
優(yōu)點(diǎn):
易理解,簡單,可靠
缺點(diǎn) :
需要了解coffeescript
整個(gè)文件都需要用coffeescript來寫
七、ES6
ES6的有一個(gè)新的特性,Template Strings, 這是語言層面上第一次實(shí)現(xiàn)了多行字符串, 在chrome canary里打開Enable Experimental JavaScript就可以使用這個(gè)特性,另外typescript也會(huì)支持這種方式
var tmpl =
`!!! 5
html
include header
body
//if IE 6
.alert.alert-error
center 對不起,我們不支持IE6,請升級(jí)你的瀏覽器
a() | IE8官方下載
a() | Chrome下載
include head
.container
.row-fluid
.span8
block main
include pagerbar
.span4
include sidebar
include footer
include script`
優(yōu)點(diǎn):
易理解,原生支持
真正的多行字符串
缺點(diǎn) :
JS引擎支持有限
八、總結(jié)
看了這么些寫法,如何選擇?如果你用的是coffeescript,放心大膽的使用它支持的多行字符串寫法;如果是在客戶端,同時(shí)你解決了你的壓縮器去掉注釋的問題,推薦使用heredoc;如果你無法解決壓縮器的問題,使用反斜線連接吧,每行只需要加一個(gè)字符。
相關(guān)文章
微信小程序?qū)崿F(xiàn)動(dòng)態(tài)渲染Markdown示例詳解
這篇文章主要為大家介紹了微信小程序?qū)崿F(xiàn)動(dòng)態(tài)渲染Markdown示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08JavaScript 變量,數(shù)據(jù)類型基礎(chǔ)實(shí)例詳解【變量、字符串、數(shù)組、對象等】
這篇文章主要介紹了JavaScript 變量,數(shù)據(jù)類型基礎(chǔ),結(jié)合實(shí)例形式詳細(xì)分析了JavaScript變量聲明、字符串、數(shù)組、對象等基本使用方法與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01layui中的tab控件點(diǎn)擊切換觸發(fā)事件
這篇文章主要介紹了layui中的tab控件點(diǎn)擊切換觸發(fā)事件,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06