模板字符串

Tags
javascript
Created
Jul 4, 2016 10:07 AM

对于同一个模版字符串,标签函数每次调用时字符串数组(不可修改)是同一个:https://github.com/Polymer/lit-html/issues/588#issuecomment-433191630

// 有提案 Array.isTempleteObject

////////// 标签函数(将模版字符串处理成参数执行这个函数)

function strongValues(strings, ...values) {

// 参数是 原始字符串的数组 和 其它模板字符串值

return strings.reduce((totalStr, str, i) => {

// strings 有两个 ["",", they are"]

totalStr += str;

if (i in values) totalStr += `<strong>${values[i]}</strong>`;

// values 只有一个元素 ["Yes"]

return totalStr; // 返回没有限制

}, '');

}

// 标签函数中允许使用 illegal escape sequences

const areThey = 'Yes';

console.log(strongValues`${areThey}, they are`);

// Logs: <strong>Yes</strong>, they are

https://jakearchibald.github.io/isserviceworkerready/demos/template-stream/

// 标签函数返回一个可读流(内容是不同的url请求可读响应流)(效果按顺序渲染;形似内容出自同一响应), 页面重复部分放在标签函数中

SuperMade with Super