如何 JSDoc 嵌套对象的方法
How do I JSDoc A Nested Object's Methods?
我一直在尝试使用 JSDoc3 在文件上生成文档,但我遇到了一些困难。 该文件(这是一个 Require.js 模块(基本上看起来像这样:
define([], function() {
/*
* @exports mystuff/foo
*/
var foo = {
/**
* @member
*/
bar: {
/**
* @method
*/
baz: function() { /*...*/ }
}
};
return foo;
}
问题是,我无法baz
显示在生成的文档中。 相反,我只得到一个foo/foo
模块的文档文件,其中列出了一个bar
成员,但bar
没有baz
(只是一个指向foo
源代码的链接(。
我尝试将bar
的指令更改为@property
,并尝试将baz
的指令更改为@member
或@property
,但这些都无济于事。 无论我做什么,巴兹似乎都不想露面。
有谁知道我可以使用什么指令结构来让 baz 出现在生成的文档中?
附言我尝试在 JSDoc 站点上阅读这样的页面 http://usejsdoc.org/howto-commonjs-modules.html,但它只描述了foo.bar
的情况,而不是foo.bar.baz
。
您可以将@module或@namespace与@memberof一起使用。
define([], function() {
/**
* A test module foo
* @version 1.0
* @exports mystuff/foo
* @namespace foo
*/
var foo = {
/**
* A method in first level, just for test
* @memberof foo
* @method testFirstLvl
*/
testFirstLvl: function(msg) {},
/**
* Test child object with child namespace
* @memberof foo
* @type {object}
* @namespace foo.bar
*/
bar: {
/**
* A Test Inner method in child namespace
* @memberof foo.bar
* @method baz
*/
baz: function() { /*...*/ }
},
/**
* Test child object without namespace
* @memberof foo
* @type {object}
* @property {method} baz2 A child method as property defination
*/
bar2: {
/**
* A Test Inner method
* @memberof foo.bar2
* @method baz2
*/
baz2: function() { /*...*/ }
},
/**
* Test child object with namespace and property def.
* @memberof foo
* @type {object}
* @namespace foo.bar3
* @property {method} baz3 A child method as property defination
*/
bar3: {
/**
* A Test Inner method in child namespace
* @memberof foo.bar3
* @method baz3
*/
baz3: function() { /*...*/ }
},
/**
* Test child object
* @memberof foo
* @type {object}
* @property {method} baz4 A child method
*/
bar4: {
/**
* The @alias and @memberof! tags force JSDoc to document the
* property as `bar4.baz4` (rather than `baz4`) and to be a member of
* `Data#`. You can link to the property as {@link foo#bar4.baz4}.
* @alias bar4.baz4
* @memberof! foo#
* @method bar4.baz4
*/
baz4: function() { /*...*/ }
}
};
return foo;
});
根据评论进行编辑:(模块的单页解决方案(
没有那个丑陋的属性表的 bar4。 即@property从 bar4 中删除。
define([], function() {
/**
* A test module foo
* @version 1.0
* @exports mystuff/foo
* @namespace foo
*/
var foo = {
/**
* A method in first level, just for test
* @memberof foo
* @method testFirstLvl
*/
testFirstLvl: function(msg) {},
/**
* Test child object
* @memberof foo
* @type {object}
*/
bar4: {
/**
* The @alias and @memberof! tags force JSDoc to document the
* property as `bar4.baz4` (rather than `baz4`) and to be a member of
* `Data#`. You can link to the property as {@link foo#bar4.baz4}.
* @alias bar4.baz4
* @memberof! foo#
* @method bar4.baz4
*/
baz4: function() { /*...*/ },
/**
* @memberof! for a memeber
* @alias bar4.test
* @memberof! foo#
* @member bar4.test
*/
test : true
}
};
return foo;
});
引用-
- 关于嵌套命名空间的另一个问题
- 对于使用命名空间的替代方法
- 记录文本对象
*注意我自己没有尝试过。请尝试分享结果。
这里有一个简单的方法:
/**
* @module mystuff/foo
* @version 1.0
*/
define([], function() {
/** @lends module:mystuff/foo */
var foo = {
/**
* A method in first level, just for test
*/
testFirstLvl: function(msg) {},
/**
* @namespace
*/
bar4: {
/**
* This is the description for baz4.
*/
baz4: function() { /*...*/ },
/**
* This is the description for test.
*/
test : true
}
};
return foo;
});
请注意,jsdoc 可以推断出baz4.baz4
和test
的类型,而不必说@method和@member。
至于让 jsdoc3 将类和命名空间的文档与定义它们的模块放在同一页面上,我不知道该怎么做。
我已经使用 jsdoc3 几个月了,用它记录了一个小型库和一个大型应用程序。我更喜欢在某些方面屈服于 jsdoc3 的意愿,而不是必须键入大量 @-指令来屈服于我的意愿。
不能直接记录嵌套函数。我不喜欢 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}
vs {Object}
(。
你也可以使用我的技术来记录闭包。
只是为了稍微改进 Prongs 对 JSDoc3 的回答,只有当我使用 @instance 注释代替@member时,我才能让它工作。
ES6 示例代码如下:
class Test
{
/**
* @param {object} something
*/
constructor(something)
{
this.somethingElse = something;
/**
* This sub-object contains all sub-class functionality.
*
* @type {object}
*/
this.topology = {
/**
* Informative comment here!
*
* @alias topology.toJSON
* @memberof! Test#
* @instance topology.toJSON
*
* @returns {object} JSON object
*/
toJSON()
{
return deepclone(privatesMap.get(this).innerJSON);
},
...
}
}
}
- 序列化数据属性中对象的最可靠方法
- 有没有一种方法可以添加相同的项目val=“0”;4〃;到JavaScript中数组的每个对象
- 如何从对象的原型方法访问JavaScript对象属性
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- 设置嵌套对象属性的更好方法
- JQuery示例不起作用-“;对象没有't支持属性或方法'按钮'&”;
- 对象不支持属性或方法“自动完成”
- call()和apply()实际上是用来欺骗方法处理类似数组的对象的
- 对象文字方法上的Javascript绑定不起作用
- Meteor应用程序无法运行-对象#<编译器>没有方法'主机'
- 为什么我可以在Array属性对象中找到Javascript Array for Each方法
- Underscore.js某些对象的所有方法的总和
- 对象#<XMLHttpRequest>没有方法'完成'
- Es6:能够在设置/更新/删除对象属性时调用自定义方法
- 丢失对象“;这个“;方法中的上下文
- 如何在ajax中调用javascript对象的方法
- 为什么页面方法对象未定义
- 如何使用在另一个文件中定义的JavaScript方法/对象
- 如何告诉JsHint忽略“未解析的函数或方法”?对象上的警告
- 使用jinja/javascript动态创建方法/对象