2015-09-13 36 views
7

为了避免在我的JavaScript代码中使用new,我编写了工厂来创建对象。用JSDoc记录工厂

我已经尝试了多种组合,并且给了我最满意的结果之一是:

/** 
* Document module 
* @module app/document 
*/ 
(function() { 
    'use strict'; 

    /** 
    * Factory that creates a document object. 
    * @alias module:app/document.factory 
    * @return {document} 
    */ 
    function document() { 
     /** 
     * Get document id 
     * @method id 
     * @return {String} 
     */ 
     var id = function id() {...}, 
      api = { 
       id: id 
      }; 

     return api; 
    } 

    /** 
    * This module exports the {@link module:app/document.factory|factory} function. 
    */ 
    module.exports = document; 
}()); 

的问题,这些意见是没有定义document对象。因此,我不能在另一个对象中引用此对象,并且在扩展此对象时我无法继承它的文档。

什么是适当的方式来记录这种类型的对象?

如果我使用@typedef标签,我得到的静态factory方法 和document对象正确记录 而是由JSDoc不会产生id方法的文档:

/** 
* Document module. 
* @module app/document 
*/ 
(function() { 
    'use strict'; 

    /** 
    * Factory that creates a document object. 
    * @function module:app/document.factory 
    * @return {document} 
    */ 
    function factory(agent) { 
     /** 
     * @callback document~id 
     * @returns {String} 
     */ 
     var id = function id() {...}, 

      /** 
      * @typedef document 
      * @property {document~id} id 
      */ 
      document = { 
       id: id 
      }; 

     return document; 
    } 

    module.exports = factory; 
}()); 
+0

尽量不要哟使用保留的关键字作为文档或功能的ID – Sagi

+0

使用@typedef - 这将允许您引用它 –

+0

@ elad.chen我编辑了问题以尝试与@ @ typedef',但我没有得到预期的结果。 – goriol

回答

1

我给你的建议是很好定义模块上的输出使用@typedef来定义类型,然后使用@type {FactoryDe​​finition}标注模块.exports =工厂

/** @typedef {{ id: !string }} */ 
var DocumentDefinition; 

/** @typedef {!function(!object):!DocumentDefinition} */ 
var FactoryDefinition; 

/** @type {FactoryDefinition} */ 
module.exports = factory