gpt4 book ai didi

javascript - Visual Studio - javascript 自定义对象的智能感知

转载 作者:可可西里 更新时间:2023-11-01 02:30:30 25 4
gpt4 key购买 nike

我创建了以下 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 的参数的智能感知:

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

This thread显示了如何为简单的 args(stringint、...)执行此操作,但如何为复杂的对象执行此操作?我正在使用 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。

我认为你最好的选择是定义一个自定义对象作为该函数的参数对象,毕竟如果你想创建一个自定义对象作为参数,这就是你在其他语言中的做法智能感知。它可能看起来像这样:

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 的副本来测试这些。

关于javascript - Visual Studio - javascript 自定义对象的智能感知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16709511/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com