
我可以看到三种解决方法:
- 使用模板字符串,就像设计使用的那样,没有任何
format
功能:console.log(`Hello, ${"world"}. This is a ${"test"}`);// might make more sense with variables:
var p0 = “world”, p1 = “test”;
console.log(Hello, ${p0}. This is a ${p1});
甚至实际推迟评估的 功能参数 :
const welcome = (p0, p1) => `Hello, ${p0}. This is a ${p1}`;console.log(welcome("world", "test"));不要使用模板字符串,而应使用纯字符串文字:
String.prototype.format = function() {var args = arguments;return this.replace(/${p(d)}/g, function(match, id) { return args[id];});};
console.log(“Hello, ${p0}. This is a ${p1}”.format(“world”, “test”));使用带标签的模板文字。请注意,替换仍将在不被处理程序拦截的情况下进行求值,因此您不能像
p0
没有变量so 那样使用标识符。如果接受了不同的 替换主体语法 建议,则此行为可能会更改(更新:否)。
function formatter(literals, ...substitutions) {return { format: function() { var out = []; for(var i=0, k=0; i < literals.length; i++) { out[k++] = literals[i]; out[k++] = arguments[substitutions[i]]; } out[k] = literals[i]; return out.join(""); }};}console.log(formatter`Hello, . This is a `.format("world", "test"));// Notice the number literals: ^ ^欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)