JS里并没有标准的多行字符串的表示方法,但是在用模板的时候,为了保证模板的可阅读性,我们又不可避免的使用多行字符串,所以出现了各种搞法,这里以一段jade的模板作为示例,简单总结和对比一下。

字符串相加

这是最容易理解也很常用的一种形式,如下

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'

优点:

  • 易理解,简单,可靠
  • 足够灵活,可以在单个字符串中添加js逻辑

缺点 :

  • 并不是真正意义上的多行字符串, 如果想要真正的多行,需要自己加\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'

优点:

  • 简单,每一行只需要有多一个\
  • 高效!在大部分的浏览器上,这种方式都是最快的,看这个效率对比

缺点 :

  • 致命缺陷,每一行的\必须不可以有空格,否则直接脚本错误
  • 并不是真正意义上的多行字符串, 如果想要真正的多行,需要自己加\n
  • 尽管绝大部分的js引擎都支持它,但是它并不是ECMAScript的一部分(我并没有研究规范,这句话是从google的编码规范翻译过来的)

字符串数组join

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');

优点:

  • 真正意义上的多行字符串
  • 易理解,简单,可靠
  • 足够灵活,可以在单个字符串中添加js逻辑

缺点 :

  • 大量的,号和'", 丑陋

String.prototype.concat

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');

优点:

  • 不常用,事实上字符串的concat方法远没有+号常见
  • 易理解,简单,可靠
  • 足够灵活,可以在单个字符串中添加js逻辑

缺点 :

  • 并不是真正意义上的多行字符串
  • 大量的,号和'", 丑陋

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,请升级你的浏览器
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可以通过/*!来避免被压缩掉,uglifyjsgcc也可以通过选项配置不删除特定的注释,这个不是大问题

coffeescript

相当于换了一个语言,其实这种语言上缺少的功能,通过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
"""

优点:

  • 易理解,简单,可靠

缺点 :

  • 需要了解coffeescript
  • 整个文件都需要用coffeescript来写

ES6

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`

优点:

  • 易理解,原生支持
  • 真正的多行字符串

缺点 :

  • JS引擎支持有限

总结

看了这么些写法,如何选择?如果你用的是coffeescript,放心大胆的使用它支持的多行字符串写法;如果是在客户端,同时你解决了你的压缩器去掉注释的问题,推荐使用heredoc;如果你无法解决压缩器的问题,使用反斜线连接吧,每行只需要加一个字符。

js与多行字符串的更多相关文章

  1. js ES6 多行字符串 连接字符串

    1. 以前,js多行字符串用\n写起来比较费事,所以最新的ES6标准新增了一种多行字符串的表示方法,用` ... `表示: 旧版写法 alert("你好,\n 我叫\n Olive" ...

  2. js中多行字符串拼接

    前言 我们会经常遇到这样的场景,需要拼接多行字符串,在字符串中动态插入一些数据以达到业务的需求.但是js中并没有标准的多行编辑的函数,于是聪明的程序员们便脑洞大开,书写出许多有趣的方法. 1 2 3 ...

  3. js定义多行字符串

    js本身没有提供类似的定义方式,但是可以通过多行注释(/* */),已经借助function的方式来达到多行字符的定义,例如代码: var jstr = function() { var fun = ...

  4. js 创建多行字符串

    function heredoc(fn) { ,-).join('\n') + '\n' } var tmpl = heredoc(function(){/* !!! 5 html include h ...

  5. js对多行字符串的处理

    f = [] g = str(f) h = ''.join(f) dic_ = () gd = str(dic_) hd = ''.join(dic_) 0 老板1 北京2 上海3 天津4 重庆5 河 ...

  6. python和js分别在多行字符串中插入一行字符串

    问题 一个多行字符串,"asfdb;\nwesfpjoing;\nwbfliqwbefpwqufn\nasfdwe\nsafewt\nqwern\nvar\ntgwtg\n\nftwg\n& ...

  7. javascript创建多行字符串的方法(转)

    JS里并没有标准的多行字符串的表示方法,但是在用模板的时候,为了保证模板的可阅读性,我们又不可避免的使用多行字符串,所以出现了各种搞法,这里以一段jade的模板作为示例,简单总结和对比一下. 一.字符 ...

  8. JS 利用正则表达式替换字符串

    JS 利用正则表达式替换字符串 博客分类: JavaScript 学习资料 Java代码 收藏代码 JS 利用正则表达式替换字符串 var data = "123123,213,12312, ...

  9. javascript的几种使用多行字符串的方式

    JS里并没有标准的多行字符串的表示方法,但是在用模板的时候,为了保证模板的可阅读性,我们又不可避免的使用多行字符串,所以出现了各种搞法,这里以一段jade的模板作为示例,简单总结和对比一下. 字符串相 ...

随机推荐

  1. iOS如何彻底避免数组越界

    我们先来看看有可能会出现的数组越界Crash的地方: ? 1 2 3 4 5 6 7 - (void)tableView:(UITableView *)tableView didSelectRowAt ...

  2. 报错注入分析之updatexml注入

    PS:今天元旦,家里打来电话说,今年春节要回老家.心里倍感恐惧.可以清楚的感觉得到父母说话的气息没有底气.大概如同我一样是恐惧吧.加油吧!努力赚钱! 先丢一篇很不错的文章:http://www.moo ...

  3. Toast 工具

    public class Utils { private static Toast toast; public static void showtoast(Context context,String ...

  4. Redis持久化

    Redis持久化 快照(默认) 将内存中的数据以快照的方式写入到二进制文件中,默认文件名是dump.rdb. 配置自动化做快照持久化(如redis在n秒内如果超过m个key被修改就自动做快照) sav ...

  5. 【转】使用Python matplotlib绘制股票走势图

    转载出处 一.前言 matplotlib[1]是著名的python绘图库,它提供了一整套绘图API,十分适合交互式绘图.本人在工作过程中涉及到股票数据的处理如绘制K线等,因此将matplotlib的使 ...

  6. C#中base 关键字的作用

    引用:http://msdn.microsoft.com/en-us/library/hfw7t1ce.aspx base base 关键字用于从派生类中访问基类的成员: 调用基类上已被其他方法重写的 ...

  7. GitHub for Windows呆瓜级操作1

    如何安装.注册.创建仓库等操作请参考http://www.cnblogs.com/foreveryt/p/4077380.html 1.点击右上角+号创建本地新仓库LHJ.点击Create repos ...

  8. Content-disposition

    今天查看Struts2的文件上传部分 发现有个例子开头打印的信息中有Content-Disposition,一时好奇,所以了解了一下.顺便学习一下文件上传所需要的注意事项. Content-dispo ...

  9. sql跨库查询

    ---------------------------------------------------------------------------------- --1. 创建链接服务器 --1. ...

  10. 安装两个tomcat

    编辑环境变量:vi /etc/profile 加入以下代码(tomcat路径要配置自己实际的tomcat安装目录) ##########first tomcat########### CATALINA ...