2010-07-03 60 views
47

編輯:這在技術上是一個2部分問題。我已經選擇了覆蓋此問題的一般答案,並與處理具體問題的答案相關聯。用jsdoc記錄匿名對象和函數的最佳方法

用jsdoc記錄匿名對象和函數的最佳方式是什麼?

/** 
* @class {Page} Page Class specification 
*/ 
var Page = function() { 

    /** 
    * Get a page from the server 
    * @param {PageRequest} pageRequest Info on the page you want to request 
    * @param {function} callback Function executed when page is retrieved 
    */ 
    this.getPage = function(pageRequest, callback) { 
    }; 
}; 

無論是PageRequest對象或callback中代碼存在。它們將在運行時提供給getPage()。但我想能夠定義對象和功能是什麼。

我可以逃脫創建PageRequest對象文件證明:

/** 
* @namespace {PageRequest} Object specification 
* @property {String} pageId ID of the page you want. 
* @property {String} pageName Name of the page you want. 
*/ 
var PageRequest = { 
    pageId : null, 
    pageName : null 
}; 

這很好(雖然我接受更好的方法來做到這一點)。

記錄callback函數的最佳方式是什麼?我想讓它知道,例如,回調函數是在格式的文檔中:

callback: function({PageResponse} pageResponse, {PageRequestStatus} pageRequestStatus) 

任何想法如何做到這一點?

回答

39

您可以通過使用@name標籤記錄的東西,這並不存在於代碼:

 /** Description of the function 
      @name IDontReallyExist 
      @function 
      @param {String} someParameter Description 
     */ 


     /** The CallAgain method calls the provided function twice 
      @param {IDontReallyExist} func The function to call twice 
     */ 
     exports.CallAgain = function(func) { func(); func(); } 

這裏是@name tag documentation。你也許會發現name paths也很有用。

+0

真的很整潔!一種記錄回調的好方法。 – oligofren 2013-06-19 09:06:59

+1

但我不明白這是如何工作的匿名對象?說一個發送到某個函數中的設置對象,以創建一個在當前作用域中不可見的對象。 – oligofren 2013-06-19 09:36:49

+1

如果您不想使用'@ name'標籤爲您的匿名對象命名,請描述其使用的對象,這將是設置對象示例的'@ param'標籤正文。 – Eric 2013-06-20 21:18:47

0

您可以使用@see鏈接到同一個類中的另一個方法。該方法永遠不會被使用,它只是爲了文檔的目的。

/** 
* @class {Page} Page Class specification 
*/ 
var Page = function() { 

    /** 
    * Get a page from the server 
    * @param {PageRequest} pageRequest Info on the page you want to request 
    * @param {function} callback Function executed when page is retrieved 
    * @see #getPageCallback 
    */ 
    this.getPage = function (pageRequest, callback) { 
    }; 

    /** 
    * Called when page request completes 
    * @param {PageResponse} pageResponse The requested page 
    * @param {PageRequestStatus} pageRequestStatus Status of the page 
    */ 
    //#ifdef 0 
    this.getPageCallback = function (pageResponse, pageRequestStatus) { }; 
    //#endif 
}; 

如果您正在使用某種形式的編譯系統,虛擬方法可以很容易地從構建省略。

+0

謝謝,沒有。我現在正在做這件事(沒有ifdef),它的工作原理,但我希望用戶能夠立即看到它是一個接受參數X和Y而不離開它們的位置的函數。類似於谷歌地圖API如何做。例如:http://code.google.com/apis/maps/documentation/javascript/reference.html#DirectionsService – 2010-07-06 18:42:45

+0

剛剛發現@link可以做我正在談論的內容。這不是完美的,但它的工作原理。如果其他人發現它有用,我會創建一個單獨的答案。 – 2010-07-06 19:00:04

1

@link可以添加到方法和類的內聯鏈接。

/** 
* Get a page from the server 
* @param {PageRequest} pageRequest Info on the page you want to request 
* @param {function} callback Function executed when page is retrieved<br /> 
* function({@link PageResponse} pageResponse,{@link PageRequestStatus} pageRequestStatus) 
*/ 
this.getPage = function (pageRequest, callback) { 
}; 

不理想,但它完成了工作。

1

爲此,Google Closure編譯器註釋具有Type Expressions,其中包括爲特定參數,返回類型甚至是此類指示類型的能力。許多圖書館都在關注Google Closure Compiler Annotations,因爲他們想用它來縮小他們的代碼。所以它有一些動力。缺點是我沒有看到描述的方式。

爲了提供描述,可能JSDoc Toolkit Parameters With Properties的方法將工作(看看頁面的底部)。這就是我現在正在做的事情。 JSDoc Toolkit正在準備開始使用V3,所以反饋可能會很好。

7

爲了恭維studgeek的回答,我提供了一個示例,顯示JsDoc with Google Closure Compiler可以讓你做什麼。

請注意,記錄的匿名類型將從生成的縮小文件中移除,並且編譯器確保傳入有效對象(如果可能)。但是,即使您不使用編譯器,它也可以幫助下一個開發人員和工具(如WebStorm(IntelliJ))理解它併爲您提供代碼完成。

// This defines an named type that you don't need much besides its name in the code 
// Look at the definition of Page#getPage which illustrates defining a type inline 
/** @typedef { pageId : string, pageName : string, contents: string} */ 
var PageResponse; 

/** 
* @class {Page} Page Class specification 
*/ 
var Page = function() {  
    /** 
    * Get a page from the server 
    * @param {PageRequest} pageRequest Info on the page you want to request 
    * 
    * The type for the second parameter for the function below is defined inline 
    * 
    * @param {function(PageResponse, {statusCode: number, statusMsg: string})} callback 
    *  Function executed when page is retrieved 
    */ 
    this.getPage = function(pageRequest, callback) { 
    }; 
}; 
+0

嗨,這似乎是最優雅的答案,但JSDoc輸出只包含'功能'沒有特定的參數輸入。我正在使用jsdoc'3.4.0'。這個語法不完全支持? – 2016-07-18 16:27:05

+1

@PeteV。我沒有跟上jsdoc和閉包編譯器之間的同步程度。我建議你看看替代文檔生成器,這些替代文檔生成器可以與閉包編譯器一起工作(因爲它是jsdoc標準的超集)。請嘗試http://plovr.com/,http://www.seehuhn.de/pages/jvjsdoc或https://github.com/google/closure-compiler/wiki/Create-HTML-Docs-using-JSDoc。我已經轉向使用TypeScript來爲JavaScript添加靜態類型 – 2016-07-18 16:38:04

26

您可以使用@callback@typedef

/** 
* @callback arrayCallback 
* @param {object} element - Value of array element 
* @param {number} index - Index of array element 
* @param {Array} array - Array itself 
*/ 

/** 
* @param {arrayCallback} callback - function applied against elements 
* @return {Array} with elements transformed by callback 
*/ 
Array.prototype.map = function(callback) { ... } 
+0

參考:http://usejsdoc.org/tags-callback.html – 2014-06-29 18:45:47

+2

@ChrisMoschini謝謝。答案中的@ callback標籤鏈接到相應的文檔頁面。 – kzh 2014-06-30 13:30:06