2015-04-27 28 views
0

我試圖用JSDoc3記錄一些舊代碼,並且我試圖讓它在文檔中包含實例方法的參數 - 或者將任何東西顯示爲實例屬性。我懷疑問題在於代碼沒有遵循在javascript中僞造類的預期習慣用法,但我想在我開始重寫任何內容之前將所有內容都記錄下來。我試圖使問題的一個小例子,與實際代碼的結構:jsdoc:如何獲取實例方法參數?

/** 
* Global function 
* @param {Object} v Stuff that they're trying to avoid making global 
* @return {Object} Updated v 
*/ 
jsdoc_test = function(v) { 
    /** 
    * Some stuff is defined in this namespace 
    * @namespace space 
    */ 
    var space = {}; 
    /** 
    * Something that acts like a class 
    * @name space.someclass 
    * @memberOf space 
    * @constructor 
    * @type {function} 
    * @param {any} y blah blah 
    * @return {Object} The constructed object 
    */ 
    space.someclass = function(w) { 
     var obj = { 
      source: w,  // might need this again 
      derived: foo(w), // what we usually need 
      etc:  "etc"  // and so on 
     }; 
     /** 
     * This should be a member function, but it appears as a static property 
     * @name space.someclass.methodA 
     * @memberOf space.someclass 
     * @type {function} 
     * @instance 
     * @param {any} x Parameters do not appear in documentation 
     * @return {Object} this 
     */ 
     obj.methodA = function(x) { 
      bar(x); // or whatever methodA does 
      return this; 
     } 
     /** 
     * This should be a member function, but it doesn't show up at all 
     * @name space.someclass.methodB 
     * @memberOf space.someclass# 
     * @type {function} 
     * @param {any} y Parameters do not appear in documentation 
     * @return {Object} this 
     */ 
     obj.methodB = function(y) { 
      baz(y); // or whatever methodB does 
      return this; 
     } 
     return obj; 
     /** 
     * This should be a member function, but it doesn't show up at all 
     * @name space.someclass.methodC 
     * @memberOf space.someclass.prototype 
     * @type {function} 
     * @param {any} z Parameters do not appear in documentation 
     * @return {Object} this 
     */ 
     obj.methodC = function(z) { 
      qux(z); // or whatever methodC does 
      return this; 
     } 
     return obj; 
    } 
    // ... 
} 

我想所有這三種方法出現在生成的文檔爲實例方法淺析。正因爲如此,methodA顯示爲靜態屬性,而methodBmethodC(其遵循here建議)完全不

我怎麼JSDoc3記錄實例方法,與它們的參數出現,而無需重寫代碼?

+0

你嘗試記錄'VAR OBJ = ...''通過/ ** * @class /?這樣,你應該能夠正確地引用它來添加屬性或返回值。 – SGD

+0

根據http://usejsdoc.org/tags-class.html,「@ class」是「@ constructor」的同義詞,我已經在應用於工廠功能。用'@ class'替換'@ constructor'和/或將註釋塊移到對象上對生成的文檔沒有影響。該類已經作爲一個類出現在文檔中,問題是實例方法顯示爲靜態屬性。 – ShadSterling

+0

是的,但你不記錄'obj'。我的建議是通過'@class'在函數內部記錄'obj',然後引用該內部類作爲函數的返回值.. – SGD

回答

0

看起來你在代碼上使用了太多的標籤。當您使用@constructor時,您不應該需要@name@type,因爲這些都是使用構造函數覆蓋的。

所以,我認爲你有兩個選擇。

使用@constructor,並刪除冗餘(發生衝突)標籤:

/** 
* Something that acts like a class 
* @constructor space.someclass 
* @memberOf space 
* @param {any} y blah blah 
* @return {Object} The constructed object 
*/ 

或者,如果你不想使用@constructor標籤,添加適當的暗示自己:

/** 
* Something that acts like a class 
* @name space.someclass 
* @memberOf space 
* @kind class 
* @param {any} y blah blah 
* @return {Object} The constructed object 
*/ 

在這兩種情況下,@type都是多餘的,因爲你正在記錄一個類;該類型在技術上將是您的函數的全名(即,@type {space.someclass})。

0

@instance的組合,@memberOf & @method應該這樣做:

/** 
* This should now be a member function. 
* @instance 
* @memberOf space.someclass 
* @method methodA 
* @param {*} x Some parameter of any type. 
* @return {Object} this. 
*/