console
//实例:模板编译
/*let template = `
<ul>
<% for(let i=0;i<data.supplies.length;i++){%>
<li><%=data.supplies[i] %></li>
<%}%>
</ul>
`;
function complie(template) {
let evalExpr = /<%=(.+?)%>/g;
let expr = /<%([\s\S]+?)%>/g;
template = template
.replace(evalExpr, '`); \n echo($1); \n echo(`')
.replace(expr, '`); \n $1 \n echo(`');
template = 'echo(`' + template + '`)';
let script = `
(function parse(data){
let output="";
function echo(html){
output+=html;
}
${template}
return output;
})
`;
return script;
}
let parse = eval(complie(template));
$('#tmp').html(parse({ supplies: [ "broom", "mop", "cleaner" ] }));*/
//标签模板
let total = 30;
function passthru(literals) {
let result = '';
let i = 0;
while (i < literals.length) {
result += literals[i++];
if (i < arguments.length) {
result += arguments[i];
}
}
return result;
}
console.log`${passthru`The total is ${total} (${total * 1.05} with tax)`}`;
function passthruRestStyle(literals, ...values) {
let output = "";
let index;
for (index = 0; index < values.length; index++) {
output += literals[index] + values[index];
}
output += literals[index];
return output;
}
passthruRestStyle`The total is ${total} (${total * 1.05} with tax)`
//console.log`${passthruRestStyle`The total is ${total} (${total * 1.05} with tax)`}`;
<html>
<title></title>
<body>
<div id="tmp"></div>
</body>
</html>