JSDoc侧边栏中的嵌套方法

Nested Methods in sidebar of JSDoc

本文关键字:嵌套 方法 侧边栏 JSDoc      更新时间:2023-09-26

感谢这里找到的答案:

https://stackoverflow.com/a/19336366/592495

我的JavaScript文档组织良好,格式良好。每个命名空间都是其中包含的方法的"父"。然而,导航并不像我希望的那样精细。

通过一个简单的命令(jsdoc file1.js file2.js)使用node.js工具编译/渲染后,文档被生成为默认模板。这个默认模板在侧边栏导航栏中显示我的名称空间,但它不显示每个名称空间包含的方法。

您可以通过向每个方法添加@class指令来创建一个方法列表,但正如我们所知,它们并不是真正的类。

我希望看到这样的侧边栏导航:

My Project
 - namespace 1
    - method.a
    - method.b
    - method.c
 -namespace 2
    - method.d
    - method.e

对于我所忽略的文档的任何指导都将非常感激。


[edit to add:]

经过实验,@class几乎完全符合我想要的,但有一些例外:

  • 它列出了名称空间上面的类。我不喜欢这样,因为名称空间就像"父母"一样。

  • JavaScript在这个意义上没有类。而不是用这种命名法称为"类"的那些。当读取文档时,看到的是一个"类"列表,这会造成一个奇怪的断开。

  • 自动添加"new"操作符。不是所有的方法都有构造函数…你可以看到问题所在!


[edit:示例代码]

这是当前的结构。在我用JSDoc注释注释它之前,下面是基本方法:

var app =  app || {};
app.utils = {
    whizbang: function() {},
    geegolly: function() {}
  };
app.render = {
    thestuff: function(params) {},
    thethings: function(params) {}
  }
}

因此,使用对象文字表示法,顶层是整个应用程序的"名称空间",但在其中有用于不同目的的子名称空间。这里,我有一个特定于实用程序的子名称空间,还有一个特定于呈现的子名称空间。每个都可以有属性,但更重要的是它们都包含函数。这些函数应该出现在侧边栏中。现在用JSDoc的当前模式来充实它:

/** 
 * @file MyApp.js This is an awesome description of MyApp.js
 * 
 * @version 0.1
 * @author Greg Pettit
 * @copyright 2015
 * 
 */
/**
 * Description of my main namespace!
 * 
 * @namespace app
 */
var app = app || {};
/**
 * This is a description of my sweet utilities namespace!
 *                                                                              
 * @memberof app
 * @type {object}
 * @namespace app.utils
 */
app.utils = {
  /**
   * app.utils.whizbang is an awesome function with magical powers. I sure wish
   * it would appear in the sidebar as a member of app.utils!
   * 
   * @memberof app.utils
   * @method whizbang
   * 
   * @param {method} [successCallback] Method invoked on successful attempt.
   * @param {method} [errorCallback] Method invoked on unsuccessful attempt.
   * 
   */
   whizbang: function(successCallback, errorCallback) { // do utility stuff! }
}
/**
 * This is a description of the best rendering namespace ever.
 *                                                                              
 * @memberof app
 * @type {object}
 * @namespace app.render
 */
app.render = {
  /**
   * app.render.thethings renders the things! I wish it would render to the sidebar...
   * 
   * @memberof app.render
   * @method thethings
   * 
   * @param {method} node The node to which thethings are rendered
   * 
   */
   thethings: function(node) { // do rendering stuff! }
}

您是否尝试使用@lends标记?这里有一个代码和文档注释的示例。

因为我不知道你的代码是什么样子的,我就举一个例子来说明我是如何在我们的内部框架中使用JSDoc的,它有很多特性(嘿,我没有写它,我只是不得不使用它)。

只是给一些上下文,我们有一个context对象,可以创建应用程序和模块(应用程序只是模块与start方法):

/** @namespace MyApp */
var MyApp = context.app('myApp').use('module1', 'module2', 'underscore');

我们的主干有一个依赖注入系统,它使用angular风格的模式来表达依赖:

/** 
* The constructor for MyModel
* @memberof MyApp
* @param {object?} attrs
* @param {object?} options
* @param {AppUtils} appUtils
* @constructor
*/  
MyApp.MyModel = function(attrs, options, appUtils) {
    this.options = options;
    this.utils = appUtils;
}
// This is injected by the dependency resolver at instantiation time
// No additional JSDoc comments are necessary, it actually gets this right
MyApp.MyModel.prototype = {
    idAttribute: 'customId',
    defaults: {
        customId: '',
        name: '',
        birthday: null
    }
};
// Registers MyModel with the IOC container as 'myModelName'
MyApp.model('myModelName', [
    'attrs',
    'options',
    'appUtils'
    MyApp.MyModel
]);

然后另一个文件可以通过将myModelName的实例添加到底部的依赖数组中来注入。

有趣的是,JSDoc实际上很好地理解了这种特殊的安排,只要我不太花哨…但是下面的模式显然对它来说太混乱了:

/**
 * @memberof MyApp
 * @param {MyApp.MyModel} myModel
 * @param {urlParser} urlParser
 * @constructor
 */
MyApp.SomeService = function(myModel, urlParser) {
    return {
        foo: function() {
            //whatever
        },
        bar: function() {
            //whatever
        }
    };
};
MyApp.service('someModuleName', [
    'myModelName',
    'urlParser',
    MyApp.SomeService
]);

我发现唯一能给我提供接近期望输出的东西是使用@lend标记告诉JSDoc特定对象/属性/方法被"借出"为不同的属性。例如,要记录主干模型的attributes属性(表面上是由它的defaults属性定义的),我们这样做:

MyApp.MyModel.prototype = {
    idAttribute: 'customId',
    /** @lends MyApp.MyModel.prototype.attributes */
    defaults: {
        customId: '',
        name: '',
        birthday: null
    }
};

在这种情况下,服务返回一个对象,我们找到的唯一方法来获得这些对象属性文档是这样的:

/**
 * @memberof MyApp
 * @param {MyApp.MyModel} myModel
 * @param {urlParser} urlParser
 * @constructor
 */
MyApp.SomeService = function(myModel, urlParser) {
    /** @lends  MyApp.SomeService.prototype */
    return {
        foo: function() {
            //whatever
        },
        bar: function() {
            //whatever
        }
    };
};

我不知道是否有任何有用的,但也许它会给你一些想法的事情,你可以尝试@lends。如果你能提供一些示例代码,我可能会给你一个更有用的答案。