3

I want to take advantage of visual studio intellisense therefore I have read:

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

Anyways why I do not get intellisense with:

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?

How can I tell visual studio know that the function is going to return an object of TYPE Customer? For example if I replace the function Test for: function Test(){ return new Customer(); } then intellicense will work.


Edit

My Goal at the end is to have something like:

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 :(
    }
});

I get a lot of json objects and I will like to cast them using this helper functions.


Temporary solution:

This is what I ended up doing:

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!!!!
    }
});
Tono Nam
  • 34,064
  • 78
  • 298
  • 470

2 Answers2

2

You are initializing the return value to an Object so it's on that which the Intellisense is based. If you initialize it to an empty Customer, then the Intellisense will detect that it is returning a Customer

function Test() {

    var c = new Customer(); //only for Intellisense

    $.ajax({...});

    return c;
}

Test(). //Customer members now appear

You can also use /// <param /> to specify the parameter type:

$.ajax({
    ...

        success: function (foundCustomer) {
           /// <param name='foundCustomer' type='Customer' />
           foundCustomer. //Customer members appear            
        }
});

Finally, your document.URL.length trick can also be used in the casting method:

Object.prototype.CastToCustomer = function() {

  var c = new Customer();
  if (document.URL.length) c = this;
  return c;

}
Mark Cidade
  • 98,437
  • 31
  • 224
  • 236
  • Yeah I love `if (document.URL.length) c = this;` I will just use it before to make the cast and avoid having to write things like: `/// ` – Tono Nam Oct 17 '13 at 16:02
0

If you change your Customer function to accept arguments:

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];
    }
}

Then inside your Test function, change c = foundCustomer to c = new Customer(foundCustomer). I'm guessing this might trigger the intellicense?

Todd
  • 1,674
  • 13
  • 13