【问题标题】:Visual Studio - intellisense for javascript custom objectVisual Studio - 用于 javascript 自定义对象的智能感知
【发布时间】:2013-05-18 13:15:14
【问题描述】:

我创建了以下 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 cmets,以便我可以为传递给函数 setProductAsPreviewed 的参数获取智能感知:

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

This thread 展示了如何处理简单的 args(stringint,...),但是如何处理复杂的对象呢?我正在使用 Visual Studio 2010。

【问题讨论】:

  • 您的args 对象是动态生成的,还是有定义类型的自定义函数?
  • 当我调用函数productManager.setProductAsPreviewed 时,它们是动态生成的。首先定义一个具有所需属性的对象以获得智能感知是个好主意吗?我试图避免这种情况,因为这是我将使用此对象的唯一地方。
  • 将我的评论更改为答案。我可能错了,因为我对 JS 智能感知的大部分知识都来自 VS2012,所以我会花点时间让其他人思考一下 =]
  • 只是快速浏览了一下并进行了一些谷歌搜索,在这方面找不到任何有用的东西,即使是 MSDN 文档也对此非常不利,尽管看起来它们确实只支持内联函数文档,因为它们只提到 <param><field><returns>msdn.microsoft.com/en-us/library/bb514138(v=vs.100).aspx

标签: javascript .net visual-studio-2010 visual-studio intellisense


【解决方案1】:

据我所知,如果将泛型变量用作参数,您无法告诉 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 的副本来测试这些。

【讨论】:

  • 感谢肖恩的努力,但这无济于事。我有一个具有公共功能的自定义对象,我需要它们的智能感知。另外,我不想更改函数的签名,我想保留一个参数对象。
  • 不知道您可以指定特定类型数组的参数/返回 - Intellisense 很棒!
【解决方案2】:

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

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

我相信这应该通过一些修改来工作

【讨论】:

    猜你喜欢
    • 2014-11-25
    • 2010-12-03
    • 2016-03-20
    • 2022-12-03
    • 2011-10-14
    • 1970-01-01
    • 2018-02-06
    • 2022-07-19
    • 2017-01-25
    相关资源
    最近更新 更多