Template literal tags是特殊类型的函数,它们将字符串数组作为第一个参数,并将其后的任意数量的占位符作为参数。当定义一个函数用于模板文字时,由于它是一种非常特定的函数形式,因此通常仅用于此目的。
是否有任何标记或首选方法使用JsDoc标记功能?
以下是此类功能的示例:
/**
* What goes here?
*/
function withoutWhiteSpace(strings, ...placeholders) {
let withSpace = strings.reduce((result, string, i) => (
result + placeholders[i - 1] + string
));
let withoutSpace = withSpace.replace(/\s\s+/g, ' ');
return withoutSpace;
}
let lines = 3;
let exampleString = withoutWhiteSpace`This is a string created with the template
literal syntax. It's really long and stretches over ${lines} lines, so a tag
is used to remove line breaks and whitespace.`
// exampleString = "This is a string created with the template literal syntax. It's really long and stretches over 3 lines, so a tag is used to remove line breaks and whitespace."