以前我是这样做
var str = "Hello " + name;
后来我知道可以这样做:
var arr = [
'Hello ',
name
];
var str = arr.join('');
但是,复杂的拼接就变成了这样 :
var str = '' +
"<!doctype html>" +
"<html>" +
" <head>" +
" <title>" + title + "</title>" +
" </head>" +
" <body>" +
" <h1>Hello " + name + ", I'm " + age + " years old. </h1>" +
" </body>" +
"</html>" +
";
其实我要和大家分享的是 https://github.com/song940/multiline.js 这个库。
他可以让这种工作变简单 . 就像这样 :
var str = multiline(function(){/*
<!DOCTYPE html>
<html>
<head>
<title>#{ title }</title>
</head>
<body>
<h1>Hello #{name}, I'm #{age} years old. </h1>
</body>
</html>
*/}, { name: 'lsong', age: 25, title: 'Homepage' });
console.log(str);
欢迎 Star & Fork .