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.
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.
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!!!!
}
});
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With