首页 > 解决方案 > 有没有办法使用 JsDoc 将函数标记为模板文字标记?

问题描述

模板字面量标签是特殊类型的函数,它以字符串数组作为第一个参数,然后是任意数量的占位符。当一个函数被定义为用于模板文字时,它通常仅用于该目的,因为它是一种非常特殊的函数形式。

是否有任何标签或首选方法可以使用 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."

标签: javascriptecmascript-6documentationjsdoctemplate-literals

解决方案


推荐阅读