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

字符串相加

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

  1. var tmpl =''+
  2. '!!! 5' +
  3. 'html' +
  4. ' include header' +
  5. ' body' +
  6. ' //if IE 6' +
  7. ' .alert.alert-error' +
  8. ' center 对不起,我们不支持IE6,请升级你的浏览器' +
  9. ' a(href="http://windows.microsoft.com/zh-CN/internet-explorer/download-ie") | IE8官方下载' +
  10. ' a(href="https://www.google.com/intl/en/chrome/browser/") | Chrome下载' +
  11. ' include head' +
  12. ' .container' +
  13. ' .row-fluid' +
  14. ' .span8' +
  15. ' block main' +
  16. ' include pagerbar' +
  17. ' .span4' +
  18. ' include sidebar' +
  19. ' include footer' +
  20. ' include script'

优点:

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

缺点 :

  • 并不是真正意义上的多行字符串, 如果想要真正的多行,需要自己加\n
  • 大量的+号看上去满天星,大量的'", 丑陋

反斜线

这个叫续行符, 这个并非一种很常见的方式, 但是一旦用上了,还是很容易上瘾,只需要加一个字符

  1. var tmpl ='\
  2. !!! 5\
  3. html\
  4. include header\
  5. body\
  6. //if IE 6\
  7. .alert.alert-error\
  8. center 对不起,我们不支持IE6,请升级你的浏览器\
  9. a(href="http://windows.microsoft.com/zh-CN/internet-explorer/download-ie") | IE8官方下载\
  10. a(href="https://www.google.com/intl/en/chrome/browser/") | Chrome下载\
  11. include head\
  12. .container\
  13. .row-fluid\
  14. .span8\
  15. block main\
  16. include pagerbar\
  17. .span4\
  18. include sidebar\
  19. include footer\
  20. include script'

优点:

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

缺点 :

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

字符串数组join

  1. var tmpl = [
  2. '!!! 5' ,
  3. 'html' ,
  4. ' include header' ,
  5. ' body' ,
  6. ' //if IE 6' ,
  7. ' .alert.alert-error' ,
  8. ' center 对不起,我们不支持IE6,请升级你的浏览器' ,
  9. ' a(href="http://windows.microsoft.com/zh-CN/internet-explorer/download-ie") | IE8官方下载' ,
  10. ' a(href="https://www.google.com/intl/en/chrome/browser/") | Chrome下载' ,
  11. ' include head' ,
  12. ' .container' ,
  13. ' .row-fluid' ,
  14. ' .span8' ,
  15. ' block main' ,
  16. ' include pagerbar' ,
  17. ' .span4' ,
  18. ' include sidebar' ,
  19. ' include footer' ,
  20. ' include script'].join('\n');

优点:

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

缺点 :

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

String.prototype.concat

  1. var tmpl = String.prototype.concat.call(
  2. '!!! 5' ,
  3. 'html' ,
  4. ' include header' ,
  5. ' body' ,
  6. ' //if IE 6' ,
  7. ' .alert.alert-error' ,
  8. ' center 对不起,我们不支持IE6,请升级你的浏览器' ,
  9. ' a(href="http://windows.microsoft.com/zh-CN/internet-explorer/download-ie") | IE8官方下载' ,
  10. ' a(href="https://www.google.com/intl/en/chrome/browser/") | Chrome下载' ,
  11. ' include head' ,
  12. ' .container' ,
  13. ' .row-fluid' ,
  14. ' .span8' ,
  15. ' block main' ,
  16. ' include pagerbar' ,
  17. ' .span4' ,
  18. ' include sidebar' ,
  19. ' include footer' ,
  20. ' include script');

优点:

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

缺点 :

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

heredoc

这是一种很有技巧的解决办法, 利用了function的toString方法

  1. function heredoc(fn) {
  2. return fn.toString().split('\n').slice(1,-1).join('\n') + '\n'
  3. }
  4. var tmpl = heredoc(function(){/*
  5. !!! 5
  6. html
  7. include header
  8. body
  9. //if IE 6
  10. .alert.alert-error
  11. center 对不起,我们不支持IE6,请升级你的浏览器
  12. a(href="http://windows.microsoft.com/zh-CN/internet-explorer/download-ie") | IE8官方下载
  13. a(href="https://www.google.com/intl/en/chrome/browser/") | Chrome下载
  14. include head
  15. .container
  16. .row-fluid
  17. .span8
  18. block main
  19. include pagerbar
  20. .span4
  21. include sidebar
  22. include footer
  23. include script
  24. */});

优点:

  • 模板字符串内不必写多余的任何字符,干净,简单
  • 真正意义上的多行字符串, 有\n

缺点 :

  • 不可以在单个字符串中添加js逻辑
  • 容易被压缩器压缩掉,yui compressor可以通过/*!来避免被压缩掉,uglifyjsgcc也可以通过选项配置不删除特定的注释,这个不是大问题

coffeescript

相当于换了一个语言,其实这种语言上缺少的功能,通过coffeescript这种以js为编译目标的语言来实现是一种非常棒的选择。

  1. var tmpl = """
  2. !!! 5
  3. html
  4. include header
  5. body
  6. //if IE 6
  7. .alert.alert-error
  8. center 对不起,我们不支持IE6,请升级你的浏览器
  9. a(href="http://windows.microsoft.com/zh-CN/internet-explorer/download-ie") | IE8官方下载
  10. a(href="https://www.google.com/intl/en/chrome/browser/") | Chrome下载
  11. include head
  12. .container
  13. .row-fluid
  14. .span8
  15. block main
  16. include pagerbar
  17. .span4
  18. include sidebar
  19. include footer
  20. include script
  21. """

优点:

  • 易理解,简单,可靠

缺点 :

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

ES6

ES6的有一个新的特性,Template Strings, 这是语言层面上第一次实现了多行字符串, 在chrome canary里打开Enable Experimental JavaScript就可以使用这个特性,另外typescript也会支持这种方式

  1. var tmpl =
  2. `!!! 5
  3. html
  4. include header
  5. body
  6. //if IE 6
  7. .alert.alert-error
  8. center 对不起,我们不支持IE6,请升级你的浏览器
  9. a(href="http://windows.microsoft.com/zh-CN/internet-explorer/download-ie") | IE8官方下载
  10. a(href="https://www.google.com/intl/en/chrome/browser/") | Chrome下载
  11. include head
  12. .container
  13. .row-fluid
  14. .span8
  15. block main
  16. include pagerbar
  17. .span4
  18. include sidebar
  19. include footer
  20. 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. Windows7安装 .net framework 4.0

    1.首先下载安装包.net framework 4.0点击下载即可 2.安装,双击下载好的安装包

  2. HBase 基本shell命令

  3. apache 虚拟机配置

    <VirtualHost *:80> DocumentRoot /www/htdocs/caipiao ServerName www.aaa.com ServerAlias aaa.com ...

  4. 各种开源Android 系统定制

    MIUI MIUI是由小米科技开发的Android装置系统.2016年2月24日,MIUI全球用户超过1.7亿.部分开源代码托管在GitHub 官网 国际网站 http://miuiandroid.c ...

  5. matlab更改打开时候默认路径

    每次打开matlab都会的修改默认路径,是一件有些烦恼的事情.所以,就想尝试更改默认路径 方法如下: 1.在matlab安装目录,找到toolbox文件夹,打开local文件件,打开matlabrc. ...

  6. 【openStack】Libcloud 如何支持 keystone V3?

    Examples This section includes some examples which show how to use the newly available functionality ...

  7. 《转载》Java异常处理的10个最佳实践

    本文转载自 ImportNew - 挖坑的张师傅 异常处理在编写健壮的 Java 应用中扮演着非常重要的角色.异常处理并不是功能性需求,它需要优雅地处理任何错误情况,比如资源不可用.非法的输入.nul ...

  8. 利用chrome插件批量读取浏览器页面内容并写入数据库

    试想一下,如果每天要收集100页网页数据甚至更多.如果采用人工收集会吐血,用程序去收集也就成为一个不二的选择.首先肯定会想到说用java.php.C#等高级语言,但这偏偏又有个登陆和验证码,搞到无所适 ...

  9. Sublime Text 2 设置tab空格

    打开Sublime Text 2 英文版:选择Preference-defalut 中文版:选择Preference-键绑定-默认 找到"translate_tabs_to_spaces&q ...

  10. iOS JSON、NSDictionary互转

    #import "myCode.h" @implementation myCode /*! * @brief 把格式化的JSON格式的字符串转换成字典 * @param jsonS ...