admin管理员组

文章数量:1401660

In one of my JavaScript files, I had to introduce some optional arguments so I followed this guide and arrived at the following method declaration:

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

and I can call it like:

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

and I can even remove a few arguments and place them in any order.
Now, I want to document it using JSDoc but I was confused that what would my JSDoc ment look like? Because that should define that I have to enter the parameters within curly braces, and I would also have to use a key.

If there is any other way by which I can use named arguments and/or optional arguments, then that would also be appreciated.

In one of my JavaScript files, I had to introduce some optional arguments so I followed this guide and arrived at the following method declaration:

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

and I can call it like:

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

and I can even remove a few arguments and place them in any order.
Now, I want to document it using JSDoc but I was confused that what would my JSDoc ment look like? Because that should define that I have to enter the parameters within curly braces, and I would also have to use a key.

If there is any other way by which I can use named arguments and/or optional arguments, then that would also be appreciated.

Share Improve this question asked Apr 29, 2021 at 14:05 Nalin AngrishNalin Angrish 3575 silver badges18 bronze badges 5
  • You don't (and JavaScript doesn't) have named arguments, you have a single argument that's an object. – jonrsharpe Commented Apr 29, 2021 at 14:07
  • @jonrsharpe Thanks to let me know that JS does not have named arguments but my implementation is somehow a workaround for that. So can you help in documenting it? – Nalin Angrish Commented Apr 29, 2021 at 14:12
  • My point is that what you need to research is how to document an object in JSDoc, which is surely covered by existing documentation, tutorials and Q&A. – jonrsharpe Commented Apr 29, 2021 at 14:13
  • Ok thanks, I'll try searching for a good resource. – Nalin Angrish Commented Apr 29, 2021 at 14:15
  • 1 Does this answer your question? How to describe "object" arguments in jsdoc? – outis Commented May 17, 2022 at 22:02
Add a ment  | 

2 Answers 2

Reset to default 7

Another option is naming your single parameter and specify its sub-params

/**
 * 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
}

You can use @typedef for this. Check this documentation.

For your case:

/**
 * @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
}

本文标签: javascriptHow to JSDoc named argumentsStack Overflow