简体   繁体   English

如何告诉JSDoc我将命名空间对象作为参数传递?

[英]How to tell JSDoc that I'm passing a namespace object as parameter?

I'm using an anonymous function to add methods to a namespace object. 我正在使用匿名函数将方法添加到名称空间对象。 In the end, I would want JSDoc (version 3.3) to generate documentation for MyNamespace.func1() , MyNamespace.func2() , etc. I guess I could add this information to each of the methods defined in the function, but it would be a lot easier to have JSDoc3 recognize that ns is the same as MyNamespace . 最后,我希望JSDoc(版本3.3)为MyNamespace.func1()MyNamespace.func2()等生成文档。我想我可以将此信息添加到函数中定义的每个方法中,但是它将让JSDoc3认识到nsMyNamespace相同要容易MyNamespace How do I do that? 我怎么做?

/** @namespace */
var MyNamespace = {};

// (... some code that adds to MyNamespace ...)

(function (ns) {

    /** Method description 1 */
    ns.func1 = function (val) {};

    /** Method description 2 */
    ns.func2 = function (val) {};

    // ...etc

})(MyNamespace);

您可以在JSDoc文档中看到有关别名的类似情况的示例,请参见“为命名空间的静态成员使用@alias”一节。

Apparently, this is not possible with JSDoc 3 (yet). 显然,这对于JSDoc 3是不可能的。

I ended up using a merge/extend/augment-type helper function, like this: 我最终使用了merge / extend / augment-type辅助函数,如下所示:

(function (ns) {

    extend(ns, /** @lends ns */ {

        /** Method description 1 */
        func1: function (val) {},

        /** Method description 2 */
        func2: function (val) {},

        // ...etc
    });

})(MyNamespace);

This gives JSDoc everything it needs to create the proper documentation. 这为JSDoc提供了创建正确文档所需的一切。 One minor disadvantage is that func1 will now show up as extend.func1 in the Chrome error console stack trace. 一个小缺点是func1现在将在Chrome错误控制台堆栈跟踪中显示为extend.func1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM