2011-06-25 21 views
8

有沒有辦法避免必須爲@property和@param輸入兩個單獨的行,如果在構造函數中參數和屬性是同名。JSDoc工具包 - 如何在同一行上指定@param和@property

/** 
* Class for representing a point in 2D space. 
* @property {number} x The x coordinate of this point. 
* @property {number} y The y coordinate of this point. 
* @constructor 
* @param {number} x The x coordinate of this point. 
* @param {number} y The y coordinate of this point. 
* @return {Point} A new Point 
*/ 
Point = function (x, y) 
{ 
    this.x = x; 
    this.y = y; 
} 

回答

3

你不知道。這是不可能的,因爲對象的函數和屬性的參數 - 這些是不同的變量,它們不能同時是函數參數和對象屬性。此外,這樣的文檔只會讓開發人員感到困惑,但並不能幫助理解API。

我勸你不要用@properties在這片文檔,而是使用@type:

/** 
* Class for representing a point in 2D space. 
* @constructor 
* @param {number} x The x coordinate of this point. 
* @param {number} y The y coordinate of this point. 
* @return {Point} A new Point 
*/ 
Point = function (x, y) 
{ 
    /** 
    * The x coordinate. 
    * @type number 
    */ 
    this.x = x; 

    /** 
    * The y coordinate. 
    * @type number 
    */ 
    this.y = y; 
} 

但實際上如此詳細的文檔是沒用的。我們在代碼Point.x = x中說的是什麼對於任何小學生都是清楚的。

+0

我的建議是,你只需要從構造函數中聲明jsdoc屬性(即使它只是稍後分配),避免從類中的隨機函數中創建它們。 –

相關問題