问题
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!!!!
}
});
回答1:
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;
}
回答2:
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?
来源:https://stackoverflow.com/questions/19430321/create-javascript-function-that-works-with-intellisense