javascript - 为了做“标记模板”,我不能传递一个返回模板文字作为参数的变量,这是真的吗?
问题描述
这有效:
hero = "batman";
sidekick = "robin";
villain = "the joker"
function f(strings, good, helper, bad) {
console.log(strings); //expected: " and must team up to defeat "
console.log(good); //expect: "batman"
console.log(helper);//expect: "robin"
console.log(bad);//expect: "the joker"
}
f`${hero} and ${sidekick} must team up to defeat ${villain}`;
但是,当我将模板文字存储在变量中并尝试将变量传递给函数时,它不起作用:
hero = "batman";
sidekick = "robin";
villain = "the joker"
sentence = `${hero} and ${sidekick} must team up to defeat ${villain}`;
function f(strings, good, helper, bad) {
console.log(strings); //expected: " and must team up to defeat "
console.log(good); //expect: "batman"
console.log(helper);//expect: "robin"
console.log(bad);//expect: "the joker"
}
fsentence;
有解决办法吗?
解决方案
推荐阅读
- amazon-ec2 - 使用 PSCP 从 Windows 机器到 EC2 实例的 PSCP 但文件未出现在服务器上
- algorithm - 如何在线性时间内找到无向图中不同最短路径的数量?
- javascript - 在运行时将对象动态注入变量
- awk - 为一系列行更改特定列中的字符串而不会丢失空格/格式
- python - 将对象列表更新为由 Python3 中的列表项组成的新对象
- excel - Excel作为日常计算器
- intellisense - 有没有办法让摩纳哥忽略字符串引号中的触发字符?
- conda - 如何将 conda 环境克隆到特定文件夹中?
- python - model.fit_generator 中的 class_weight 参数如何与数据生成器一起使用?
- spring - 启动时的 Spring JDBC 连接没有问题,但是当插入或更新发生问题时