据我所知,如果一个泛型变量被用作参数,你不能告诉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。
我认为最好的办法是定义一个自定义对象作为参数对象,只用于该函数。毕竟,如果想要创建一个带有智能感知的自定义对象作为参数,在其他语言中也是这样做的。它可能看起来像这样:
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;
}
您会在此处获得对象的智能感知(它将显示您放置在returns
元素中的内容):
setProductAsPreviewed(
如果您决定创建一个新对象,您会在此处获得 IntelliSense(它将逐一显示每个参数的描述,随着您添加它们):
setProductAsPreviewed(new ProductPreviewArgs(
我不确定在returns
元素上使用type
属性是否会像那样起作用,它在VS2012中确实可以,正如你现在可能已经期望的那样,这方面的文档令人恼火地简单; 而且我现在没有VS2010副本来测试任何内容。
args
对象是即时生成的,还是有自定义函数定义类型? - Sean AireyproductManager.setProductAsPreviewed
时动态生成的。是否定义一个具有所需属性的对象以便拥有智能感知是个好主意?我一直试图避免这样做,因为这是我唯一使用此对象的地方。 - Rui Jarimba<param>
、<field>
和<returns>
:http://msdn.microsoft.com/en-us/library/bb514138(v=vs.100).aspx - Sean Airey