最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSDoc required parameter with default value - Stack Overflow

programmeradmin0浏览0评论

I found the following for document an optional parameter in JSDoc.:

/**
 * @param {string} [somebody=John Doe] - Somebody's name.
 */
function sayHello(somebody) {
    if (!somebody) {
        somebody = 'John Doe';
    }
    alert('Hello ' + somebody);
}

and this for document parameters object properties.

/**
 * Assign the project to an employee.
 * @param {Object} employee - The employee who is responsible for the project.
 * @param {string} employee.name - The name of the employee.
 * @param {string} employee.department - The employee's department.
 */
Project.prototype.assign = function(employee) {
    // ...
};

Now I want document a required object parameter in JSDoc. Something like this.:

/**
 * Assign the project to an employee.
 * @param {Object} employee - The employee who is responsible for the project.
 * @param {string} employee.name=John Doe - The name of the employee.
 * @param {string} employee.department - The employee's department.
 */
Project.prototype.assign = function(employee) {
    // ...
};

How can I do that?

Maybe the solution @param {string} employee.name=John Doe - The name of the employee. is already the correct one?

I found the following for document an optional parameter in JSDoc.:

/**
 * @param {string} [somebody=John Doe] - Somebody's name.
 */
function sayHello(somebody) {
    if (!somebody) {
        somebody = 'John Doe';
    }
    alert('Hello ' + somebody);
}

and this for document parameters object properties.

/**
 * Assign the project to an employee.
 * @param {Object} employee - The employee who is responsible for the project.
 * @param {string} employee.name - The name of the employee.
 * @param {string} employee.department - The employee's department.
 */
Project.prototype.assign = function(employee) {
    // ...
};

Now I want document a required object parameter in JSDoc. Something like this.:

/**
 * Assign the project to an employee.
 * @param {Object} employee - The employee who is responsible for the project.
 * @param {string} employee.name=John Doe - The name of the employee.
 * @param {string} employee.department - The employee's department.
 */
Project.prototype.assign = function(employee) {
    // ...
};

How can I do that?

Maybe the solution @param {string} employee.name=John Doe - The name of the employee. is already the correct one?

Share Improve this question edited Nov 27, 2022 at 23:55 jonrsharpe 122k30 gold badges268 silver badges476 bronze badges asked Oct 13, 2015 at 13:26 BuZZ-dEEBuZZ-dEE 6,99116 gold badges74 silver badges105 bronze badges 2
  • Can you specify a little bit more what your goal is? I personally can't understand your question. – divoom12 Commented Oct 13, 2015 at 13:29
  • I updated the question. – BuZZ-dEE Commented Oct 13, 2015 at 13:43
Add a ment  | 

1 Answer 1

Reset to default 3

You are correct. Per "Parameters with properties" in https://jsdoc.app/tags-param.html:

If a parameter is expected to have a specific property, you can document that property by providing an additional @param tag.

However, if you're going to re-use those properties in other functions you might want to instead define an Employee type:

/**
 * The employee who is responsible for the project
 * @typedef {Object} Employee
 * @property {string} name - The name of the employee
 * etc.
 */

/*
 * @param {Employee} employee - The employee who is responsible for the project.
 */
Project.prototype.assign = function(employee) {

https://jsdoc.app/tags-typedef.html

发布评论

评论列表(0)

  1. 暂无评论