您不能直接记录嵌套函数。我不喜欢 Prongs 解决方案,所以我使用了没有命名空间的不同实现(它是 JS,而不是Java!)。
更新:
我更新了我的答案以反映 OP 给出的确切用例(这是公平的,因为 JSdoc 使用起来非常痛苦)。以下是它的工作原理:
/** @module foobar */
/** @function */
function foobarbaz() {
/*
* You can't document properties inside a function as members, like you
* can for classes. In Javascript, functions are first-class objects. The
* workaround is to make it a @memberof it's closest parent (the module).
* manually linking it to the function using (see: {@link ...}), and giving
* it a @name.
*/
/**
* Foo object (see: {@link module:foobar~foobarbaz})
* @name foo
* @inner
* @private
* @memberof module:foobar
* @property {Object} foo - The foo object
* @property {Object} foo.bar - The bar object
* @property {function} foo.bar.baz - The baz function
*/
var foo = {
/*
* You can follow the same steps that was done for foo, with bar. Or if the
* @property description of foo.bar is enough, leave this alone.
*/
bar: {
/*
* Like the limitation with the foo object, you can only document members
* of @classes. Here I used the same technique as foo, except with baz.
*/
/**
* Baz function (see: {@link module:foobar~foo})
* @function
* @memberof module:foobar
* @returns {string} Some string
*/
baz: function() { /*...*/ }
}
};
return foo;
}
不幸的是,JSdoc 是 Java 的一个端口,所以它有很多对 Java 有意义但对 JS 没有意义的特性,反之亦然。例如,由于在 JS 中函数是一等对象,它们可以被视为对象或函数。所以做这样的事情应该有效:
/** @function */
function hello() {
/** @member {Object} */
var hi = {};
}
但它不会,因为 JSdoc 将它识别为一个函数。您将不得不使用命名空间,我的技术 with @link
,或使其成为一个类:
/** @class */
function Hello() {
/** @member {Object} */
var hi = {};
}
但这也没有任何意义。JS中是否存在类? 不,他们没有。
我认为我们确实需要找到更好的文档解决方案。我什至在文档中看到了关于如何显示类型(例如{object}
vs {Object}
)的不一致。
你也可以使用我的技术来记录闭包。