3 回答

TA贡献1842条经验 获得超13个赞
b
eval
let tpl = eval('`'+a+'`');

TA贡献1802条经验 获得超4个赞
在我的项目中,我用ES6创建了这样的东西:
String.prototype.interpolate = function(params) {
const names = Object.keys(params);
const vals = Object.values(params);
return new Function(...names, `return \`${this}\`;`)(...vals);
}
const template = 'Example text: ${text}';
const result = template.interpolate({
text: 'Foo Boo'
});
console.log(result);
更新

TA贡献1866条经验 获得超5个赞
//non working code quoted from the questionlet b=10;console.log(a.template());//b:10
eval
eval()
Function()
添加回答
举报