簡體   English   中英

JSDoc如何命名為arguments

[英]How to JSDoc named arguments

在我的 JavaScript 文件之一中,我不得不引入一些可選的 arguments 因此我遵循了本指南並得出了以下方法聲明:

function my_func({
    opt1,
    opt2 = 250,
    opt3 = "A message.",
    opt4 = null
}) {
    // Do something
}

我可以這樣稱呼它:

my_func({
        opt1: "boom",
        opt4: document.getElementById("some-element"),
        opt3: "A different message.",
        opt2: 200
    });

我什至可以刪除一些 arguments 並將它們按任意順序放置。
現在,我想使用 JSDoc 記錄它,但我很困惑,我的 JSDoc 評論會是什么樣子? 因為那應該定義我必須在大括號內輸入參數,而且我還必須使用鍵。

如果有任何其他方式我可以使用命名 arguments 和/或可選的 arguments,那么我也將不勝感激。

另一種選擇是命名您的單個參數並指定其子參數

/**
 * my func has a single object argument
 * @param {Object} params
 * @param {string} params.opt1  - opt1 description
 * @param {number} [params.opt2=250] - opt2 description
 * @param {string} [params.opt3='A message'] - opt3 description
 * @param {Element|null} [params.opt4=null] - opt4 description
 */
function my_func({
  opt1,
  opt2 = 250,
  opt3 = "A message",
  opt4 = null
}) {
  // Do something
}

您可以為此使用@typedef 檢查此文檔

對於您的情況:

/**
 * @typedef {Object} NameMeAsYouLike
 * @property {string} opt1 - opt1 description
 * @property {string} [opt2=250] - opt2 description
 * @property {string} [opt3=A message.] - opt3 description
 * @property {number} [opt4=null] - opt4 description
 */

/**
 * @param {NameMeAsYouLike} name me as you like 
 */
function my_func({
    opt1,
    opt2 = 250,
    opt3 = "A message.",
    opt4 = null
}) {
    // Do something
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM