实现模版字符串
(变量,控制流语句)(replace)
// 直接改写String.prototype.render
// substring(2,match.length-2)
// startsWith('#')
// 参数为data
String.prototype.render = function (data) {
// 把包括空格的所有用两个{{包裹的部分
return this.replace(/{{[.\s\S]*?}}/g, (match) => {
if ((match = match.substring(2, match.length - 2).trim()) == "") {
return "";
} else if (match.startsWith("#")) {
// 如果是代码!就要把#以后的所有代码都eval执行的结果返回
return eval(match.substr(1, match.length - 1));
} else {
return data[match] ? data[match] : "";
}
});
};
const data = {
name: "小明",
age: 16,
school: "第三中学",
classroom: "教室2",
};
console.log(
"{{ name }} 今年 {{ age }} 岁,就读于 {{ school }} 今天在 {{ classroom }} 上课,{{ name }} {{ #data.age >= 18 ? '成年了' : '未成年' }}".render(
data
)
);
// 小明 今年 16 岁,就读于 第三中学 今天在 教室2 上课,小明 未成年
console.log(
`{{name}}说了句{{#
if (data.age >= 18) {
"我已经成年了!"
} else {
"我还没有成年!"
}
}}`.render(data)
);
// 小明说了句我还没有成年!