CoffeeScript 多行字符串编译为多行字符串

标签 coffeescript

这个字符串怎么来的

"answer 
 to life 
 the universe 
 and everything
 is
 #{40+2}
"

编译成

"  answer   to life   the universe   and everything  is  " + (40 + 2) + "";

如何强制 coffescript 保持多行(保持字符串插值完整):

 "answer \ 
 to life \
 the universe \
 and everything \
 is \
 "+(40+2)

最佳答案

尝试使用 heredoc 语法:

myString = """
answer
to life
the universe
and everything
is
#{40+2}
"""

这将转换为以下 JavaScript:

var myString;

myString = "answer\nto life\nthe universe\nand everything\nis\n" + (40 + 2);

实际上没有任何意义让它在编译后的 JavaScript 中以视觉方式出现在换行符上,不是吗?

关于CoffeeScript 多行字符串编译为多行字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19389174/

相关文章:

json - 如何使用coffeescript遍历JSON哈希

javascript - Coffeescript 存在运算符有时会检查未定义的情况,但并非总是如此

javascript - 处理数据表行内的事件

javascript - meteor .js : how to pass the data context of one helper to another helper?

javascript - 如何使用 ajax in rails 将数据从 Controller 传递到 View

javascript - 在 Meteor.js 中基于 URL 发布集合

集成 Stripe API 时 Rails 应用程序中出现 Javascript 错误

javascript - 在构造函数中定义时未定义的类变量

jquery - CSS 未检测到数据属性的更改

javascript - Emberjs 与 konacha 的集成测试