Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对象数组如何注释 #27

Open
luzhenzhen opened this issue Feb 27, 2018 · 3 comments
Open

对象数组如何注释 #27

luzhenzhen opened this issue Feb 27, 2018 · 3 comments

Comments

@luzhenzhen
Copy link

对象数组中对象的属性如何注释,格式应该是什么样子的?

@techird
Copy link
Member

techird commented Feb 27, 2018

JSDoc @typedef 嘛:

/**
 * @typedef {{ name: string; level: number }} Person
 **/

/** @types {Person[]} */
const persons = [{
   { name: "集鹄大叔", level: 9999 },
   { name: "胡子大叔", level: 1 },
}];

@luzhenzhen
Copy link
Author

luzhenzhen commented Feb 27, 2018

那作为函数参数的时候该怎么注释呢?比如:

/**
 * 数字数组参数注释
 *
 * @param {Array.<number>} nums 数字数组
 */
function showNums(nums) {
    // ...
}

/**
 * 对象参数注释
 *
 * @param {Object} user 用户
 * @param {string} user.name 用户姓名
 * @param {number} user.age 用户年龄
 */
function showUser(user) {
    // ...
}

/**
 * 对象数组参数注释
 *
 * @param {Array.<Object>} users 用户数组
 * @param {string} users[i].name 用户姓名
 * @param {number} users[i].age 用户年龄
 */
function showUsers(users) {
    // ...
}

上面的第三个函数注释格式肯定不对,应该怎么去写这种注释呢?

@luzhenzhen
Copy link
Author

emm...
查阅了一些资料,答案如下:

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

参考资料:
http://usejsdoc.org/tags-param.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants