3

我想在我們的JS庫中自動化一個特定的模塊,並卡在一個點,我想定義一組屬性(讓我們說一個對象,一類建築參數)。將子屬性添加到jsdoc中的現有屬性列表中

/** 
* This function initiates world peace! 
* @constructor 
* @param {object} defaults  - The options to initiate peace. 
* @param {number} defaults.issues - The number of issues being taken up. 
* @param {string} defaults.source - The name of the location where process starts. 
*/ 
var WorldPeace = function (defaults) { 
    // code here 
}; 

該建築的所有屬性都定義在一個位置,這是很好,很好。不幸的是,我的代碼有許多模塊對這些構建屬性有貢獻。比方說,在代碼的一些其他部分(在稍後的文件),導致有一對夫婦更多的屬性

* @param {Date} defaults.start - The date when the process started. 
* @param {Date} defaults.stop - The date when the process should stop. 

我該如何去有關添加到我的WorldPeace以前定義的原始屬性集的功能?做一些混合或子類屬性會太過分了!因此,如果我可以簡單地注入屬性列表定義,那就太好了。

回答

1

最簡單的方法是使用一個記錄類型:

/** 
* This function initiates world peace! 
* @constructor 
* @param {{issues: number, source: string}} defaults - options to initiate peace. 
*/ 
var WorldPeace = function (defaults) { 
    // code here 
}; 

你也可以實現一個接口:

/** @interface */ 
var WordPeaceDefaults; 

/** @type {number} */ 
WorldPeaceDefaults.prototype.issues; 

/** @type {string} */ 
WorldPeaceDefaults.prototype.source; 

/** 
* This function initiates world peace! 
* @constructor 
* @param {WorldPeaceDefaults} defaults - options to initiate peace. 
*/ 
var WorldPeace = function (defaults) { 
    // code here 
}; 

/** 
* @constructor 
* @implements {WorldPeaceDefaults} 
*/ 
function MyWorldPeaceDefaults() {} 

/** @type {number} */ 
MyWorldPeaceDefaults.prototype.issues = 0; 

/** @type {string} */ 
MyWorldPeaceDefaults.prototype.source = ''; 

WordPeace(new MyWorldPeaceDefaults); 
+0

接口在技術上是正確的 - 但它會再次被記錄在遠處從功能上來說 - 就像我之前提到的那樣,會過度。我仍然會看到它是否會產生比我之前做的更好的事情! :-) –

+1

接口方式與類相似地記錄事物,因此會像文檔那樣「過度」,我只想簡單地將它列爲枚舉器或屬性列表。 :( –

1

另一種方式來做到這一點是使用一個typedef:

/** 
* @typedef {{ 
* issues: number, 
* source: string 
* }} 
*/ 
var WorldPeaceOptions; 

/** 
* @constructor 
* @param {WorldPeaceOptions} defaults 
*/ 
var WorldPeace = function (defaults) { 
    // code here 
};