【发布时间】:2023-03-12 20:43:01
【问题描述】:
假设我有
const highlight = (strings, ...values) => {
// console.logs in highlight
}
假设我想创建一个修改模板然后调用 highlight 的“中间件”:
const doMore = (string, ...values) => {
// modify string/values and then call highlight here
}
所以我现在可以做类似的事情
doMore`
the string ${template}
`;
我不知道如何从doMore 调用highlight。我该怎么做?
我的第一次尝试是使用... 运算符。但这没有用。具体来说,我正在尝试为 npm chalk 应用程序创建一个包装器,所以我做了类似的事情:
const doMore = (string, ...values) => {
// extend string, values
return chalk(string, ...values)
}
但这会引发错误:chalk_1.default.apply is not a function。正常做
chalk`the string ${template}`
但使用扩展运算符调用它会引发此错误。
【问题讨论】:
-
function doMore(string, ...values){ const a = [...arguments]; highlight(a.shift(), a); }
标签: javascript tagged-templates