您不能直接记录嵌套函数。我不喜欢 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 将它识别为一个函数。你必须使用命名空间,我对@link 的技术,或者把它变成一个类:
/** @class */
function Hello() {
/** @member {Object} */
var hi = {};
}
但是那也没有任何意义。 JS 中是否存在类? 不,它们不存在。
我认为我们确实需要找到更好的文档解决方案。我什至在文档中看到了关于应如何显示类型的不一致(例如 {object} 与 {Object})。
你也可以用我的技术记录closures。