Visual Studio - 用于javascript自定义对象的intellisense

Rui*_*mba 10 .net javascript intellisense visual-studio-2010 visual-studio

我创建了以下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
    };

} (); 
Run Code Online (Sandbox Code Playgroud)

传递给的对象setProductAsPreviewed具有以下属性:

args = {
    productId: int,
    productName: string,
    updateDate: date,
    saveItems: bool
};
Run Code Online (Sandbox Code Playgroud)

我想包含XML注释,以便我可以获得传递给函数的参数的intellisense setProductAsPreviewed:

var productManager = new window.Content.ProductManager();
// show intellisense when typing the following:
productManager.setProductAsPreviewed( 
Run Code Online (Sandbox Code Playgroud)

此线程展示了如何做到这一点简单ARGS( string,int,...),但如何做到这一点的一个复杂的对象吗?我正在使用Visual Studio 2010.

Sea*_*rey 21

据我所知,如果它被用作参数,你无法告诉IntelliSense泛型变量的字段和方法.

如果变量是一个数组,你可以像这样定义它:

function funcWithArrayArg(arrayArg) {
    /// <param name="arrayArg" type="Array" elementType="Number">An array of numbers</param>
}
Run Code Online (Sandbox Code Playgroud)

在VS2012中你也可以注释对象,就像这样(你可以在用作对象构造函数的函数上注释,正如我在下面演示的那样,但文档没有说明像这样的匿名对象):

var args = {
    /// <field type="Number">Product ID</field>
    productID: int
};
Run Code Online (Sandbox Code Playgroud)

这两种方法都没有真正做到你想要的,因为第二种方法不会给你关于函数参数的智能感知,无论如何你都在使用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;
}
Run Code Online (Sandbox Code Playgroud)

你可以在这里得到intellisense对象(它将显示你在returns元素中放置的内容):

setProductAsPreviewed(
Run Code Online (Sandbox Code Playgroud)

如果您决定创建一个新对象,那么您将在此处获得IntelliSense(当您添加它们时,它将逐个显示每个参数的描述):

setProductAsPreviewed(new ProductPreviewArgs(
Run Code Online (Sandbox Code Playgroud)

我不完全确定元素type上的属性是否returns真的会像那样工作,它在VS2012中确实如此,而且正如你现在所期望的那样,文档在这个主题上是令人讨厌的; 我现在还没有VS2010的副本来测试任何一个.


Joh*_*han 6

您可以创建一个单独的IntelliSense文件,看起来像这样

intellisense.annotate(Content, {
  'setProductAsPreviewed ': function() {
    /// <signature>
    ///   <summary>Summary<summary>
    ///   <param name="args" type="ComplexObject">some text here
    /// </signature>
   }
})
Run Code Online (Sandbox Code Playgroud)

我相信这应该有一些修改