3

Visual Studio Intellisense を利用したいので、以下を読みました:

http://msdn.microsoft.com/en-us/library/bb514138.aspx

とにかく、なぜ私はインテリセンスを取得しないのですか:

function Customer() {
    this.Id = 0;
    this.FirstName = "";
    this.LastName = "";
}

function Test() {
    /// <returns type="Customer"></returns>

    var c = new Object();
    $.ajax({
        async: false,
        dataType: "json",
        url: "Ajax/GetCustomer.aspx",
        success: function (foundCustomer) {
            // I know that found customer is of type Customer
            c = foundCustomer;
        }
    });

    return c;
}

var x = Test();
x. // No intellicense! why?

関数が TYPE のオブジェクトを返すことを Visual Studio に知らせるにはどうすればよいCustomerですか? たとえば、関数 Test for: を置き換えるfunction Test(){ return new Customer(); }と、intellicense が機能します。


編集

最後の私の目標は、次のようなものにすることです。

function Customer() {
    this.Id = 0;
    this.FirstName = "";
    this.LastName = "";
}

Object.prototype.CastToCustomer = function(){
    /// <returns type="Customer"></returns>
    return this;
}

$.ajax({
    async: false,
    dataType: "json",
    url: "Ajax/GetCustomer.aspx",
    success: function (foundCustomer) {

        foundCustomer = foundCustomer.CastToCustomer();
        foundCustomer.// Intellicense does not work :(
    }
});

多くの json オブジェクトを取得し、このヘルパー関数を使用してそれらをキャストしたいと考えています。


一時的な解決策:

これは私がやったことです:

function Customer() {
    this.Id = 0;
    this.FirstName = "";
    this.LastName = "";
}

$.ajax({
    async: false,
    dataType: "json",
    url: "Ajax/GetCustomer.aspx",
    success: function (foundCustomer) {
        // this will never be true on real browser. It is always true in visual studio (visual studio will now think that found customer is of type Customer ;)
        if (document.URL.length == 0) foundCustomer = new Customer();

        foundCustomer.// Intellisense works!!!!
    }
});
4

2 に答える 2

0

引数を受け入れるように Customer 関数を変更した場合:

function Customer(opts) {
    var opts = opts || {};

    this.id = 0;
    this.firstName = '';
    this.lastName = '';

    for (var i in opts) {
        if (opts.hasOwnProperty(i)) this[i] = opts[i];
    }
}

次に、Test関数内で に変更c = foundCustomerc = new Customer(foundCustomer)ます。これがインテリセンスを引き起こす可能性があると思いますか?

于 2013-10-17T15:16:34.430 に答える