[英]Visual Studio - intellisense for javascript custom object
我創建了以下javascript對象:
var Content = Content || {};
// Constructor defines properties and inits object
Content.ProductManager = function () {
// ...
};
Content.ProductManager.prototype = function () {
//
// private members
//
var setProductAsPreviewed = function (args) {
// code omitted for brevity
// ....
};
//
// public members
//
return {
setProductAsPreviewed: setProductAsPreviewed
};
} ();
傳遞給setProductAsPreviewed
的對象具有以下屬性:
args = {
productId: int,
productName: string,
updateDate: date,
saveItems: bool
};
我想包含XML注釋,以便我可以獲得傳遞給函數setProductAsPreviewed
的參數的intellisense:
var productManager = new window.Content.ProductManager();
// show intellisense when typing the following:
productManager.setProductAsPreviewed(
這個主題展示了如何為簡單的args( string
, int
,...)做這個,但是如何為復雜的對象做呢? 我正在使用Visual Studio 2010。
據我所知,如果它被用作參數,你無法告訴IntelliSense泛型變量的字段和方法。
如果變量是一個數組,你可以像這樣定義它:
function funcWithArrayArg(arrayArg) {
/// <param name="arrayArg" type="Array" elementType="Number">An array of numbers</param>
}
在VS2012中你也可以注釋對象,就像這樣(你可以注釋用作對象構造函數的函數上的字段,如下所示,但文檔沒有說明像這樣的匿名對象):
var args = {
/// <field type="Number">Product ID</field>
productID: int
};
這兩種方法都沒有真正做到你想要的,因為第二種方法不會給你關於函數參數的智能感知,無論如何你都在使用VS2010。
我認為你最好的辦法是定義一個自定義對象,用作該函數的參數對象,如果你想用intellisense創建一個自定義對象作為參數,你可以在其他語言中使用它。 它可能看起來像這樣:
function ProductPreviewArgs(productId, productName, updateDate, saveItems) {
/// <summary>Creates an object for use as the sole argument to the setProductAsPreviewed function</summary>
/// <param name="productId" type="Number" optional="false">The Product ID</param>
/// <param name="productName" type="String" optional="false">The Product Name</param>
/// <param name="updateDate" type="Date" optional="false">The date the product was last updated</param>
/// <param name="saveItems" type="Boolean" optional="false">Specifies whether or not to save the items</param>
/// <returns type="ProductPreviewArgs">An object intended for use as the sole argument to the setProductAsPreviewed function</returns>
/// <field name="productId" type="Number">The Product ID</field>
/// <field name="productName" type="String">The Product Name</field>
/// <field name="updateDate" type="Date">The date the product was last updated</field>
/// <field name="saveItems" type="Boolean">Specifies whether or not to save the items</field>
this.productId = productId;
this.productName = productName;
this.updateDate = updateDate;
this.saveItems = saveItems;
}
你可以在這里得到intellisense對象(它將顯示你在returns
元素中放置的內容):
setProductAsPreviewed(
如果您決定創建一個新對象,那么您將在此處獲得IntelliSense(當您添加它們時,它將逐個顯示每個參數的描述):
setProductAsPreviewed(new ProductPreviewArgs(
我不完全確定returns
元素上的type
屬性是否真的會像那樣工作,它在VS2012中是這樣做的,而且正如你現在所期望的那樣,文檔在這個主題上是煩人的; 我現在還沒有VS2010的副本來測試任何一個。
您可以創建一個單獨的IntelliSense文件,看起來像這樣
intellisense.annotate(Content, {
'setProductAsPreviewed ': function() {
/// <signature>
/// <summary>Summary<summary>
/// <param name="args" type="ComplexObject">some text here
/// </signature>
}
})
我相信這應該有一些修改
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.