3

我已经能够让 javascript intellisense 为这样定义的“类”原型正常工作:

function GetCustomerList()
{
}

GetCustomerList.prototype = 
{
    HEADER: {
        RETURN_CODE: 0,
        RETURN_MESSAGE: "",
        }
    ,
    NUM_RECORDS: 0,
    START_RECORD: 0,
    END_RECORD: 0
};

我可以输入如下内容:

var req = new GetCustomerList();
req.HEADER.RETURN_CODE = 100;

Visual Studio 的智能感知知道 HEADER 属性,以及它自己的名为“RETURN_CODE”和“RETURN_MESSAGE”的属性。我可以:

req.NUM_RECORDS = 50;

智能感知完美运行。

所以智能感知适用于复杂的嵌套类型 - 很棒。但是,是否有可能通过一系列复杂类型获得智能感知?

例子:

function Customer()

Customer.prototype = {

    NAME: "",
    ADDRESS: "",
    ID: 0
};

function GetCustomerList()
{
}

GetCustomerList.prototype = 
{
    HEADER: {
        RETURN_CODE: 0,
        RETURN_MESSAGE: "",
        }
    ,
    NUM_RECORDS: 0,
    START_RECORD: 0,
    END_RECORD: 0,
    CUSTOMERS: [ new CUSTOMER() ]
};

我有一个“客户”类型的数组,我还为其定义了一个原型。我希望能够输入以下内容:

req.CUSTOMER[ 0 ].NAME 

并让智能感知提示我“NAME”是该数组可用的属性。

这可能吗?

4

3 回答 3

3

更新:

正如您已经注意到的那样,IntelliSense 适用于您的复杂类型,但不适用于 Array。即使你创建了一个像 String 这样的内在类型数组,它仍然不起作用。

我已经彻底研究过这个话题,理论上这应该是可能的,但事实并非如此。
作为测试,创建名为“customers.js ”的 javascript 文件并包含以下内容:

function Customer() {
    /// <summary>This is my custom intellisense for the Customer type</summary>
    ///<field name="NAME" type="String">The Customer's name</field>
    ///<field name="ADDRESS" type="String">The customer's address</field>
    ///<field name="ID" type="String">The ID number</field>
}
Customer.prototype = {
    NAME: "",
    ADDRESS: "",
    ID: 0
};

function CustomerList() {
    /// <summary>The List of Customers</summary>
    ///<field name="HEADER" type="String">The header</field>
    ///<field name="CUSTOMERS" type="Array" elementType="Customer" >The list of customers in an Array</field>
}

CustomerList.prototype =
{
    HEADER: {
        RETURN_CODE: 0,
        RETURN_MESSAGE: ""
    },
    NUM_RECORDS: 0,
    START_RECORD: 0,
    END_RECORD: 0,
    CUSTOMERS: [new Customer()]
};

然后在里面引用这个文件<script src="customers.js"/>

/// <reference path="customer.js" />在另一个 JS 文件中。

看看智能感知如何正确显示摘要,但是当涉及到数组时,什么也没有。

var custList = new CustomerList();

custList.CUSTOMERS // intellisense
custList.CUSTOMERS[0] // no intellisense as you already know
于 2009-04-21T14:43:26.403 回答
0

没有办法让它在 VS2008 中工作。它将在下一个版本的 VS 中工作。

于 2009-05-15T00:23:54.777 回答
0

尽管 VS 不支持它,但您可能需要考虑以下解决方法。

我将示例脚本更改为:

function Customer(obj) {
    /// <summary>This is my custom intellisense for the Customer type</summary> 
    ///<field name="NAME" type="String">The Customer's name</field> 
    ///<field name="ADDRESS" type="String">The customer's address</field>
    ///<field name="ID" type="String">The ID number</field>
    if (obj) return obj;
}

Customer.prototype = {
    NAME: '',
    ADDRESS: '',
    ID: 0
};

function CustomerList() {
    /// <summary>The List of Customers</summary> 
    ///<field name="HEADER" type="String">The header</field> 
    ///<field name="CUSTOMERS" type="Array" elementType="Customer" >The list of customers in an Array</field> 
}

CustomerList.prototype =
{
    HEADER: {
        RETURN_CODE: 0,
        RETURN_MESSAGE: ''
    },
    CUSTOMERS: []
}; 

(注意构造函数的变化——不确定智能感知的 XML 注释)。

然后你可以像这样引用它(使用智能感知):

var list = new CustomerList();

var cust = new Customer();
cust.NAME = 'john';
list.CUSTOMERS.push(cust);

var cust1 = new Customer(list.CUSTOMERS[0]);
alert(cust1.NAME);

var cust2 = new Customer({ NAME: 'Mark' });
alert(cust2.NAME);

反对这一点的论点是您需要构造函数中的参数。

于 2010-10-06T02:24:51.187 回答