问题描述
我想利用visual studio intellisense,因此我读过:
I want to take advantage of visual studio intellisense therefore I have read:
无论如何为什么我没有得到intellisense:
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?
我如何告诉visual studio知道该函数将返回TYPE <$ c的对象$ C>客户?例如,如果我替换函数Test for: function Test(){return new Customer(); }
然后intellicense将工作。
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 :(
}
});
我得到了很多json对象,我想使用这个辅助函数来强制转换它们。
I get a lot of json objects and I will like to cast them using this helper functions.
这是我最终做了什么:
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!!!!
}
});
推荐答案
您正在将返回值初始化为对象
所以这是Intellisense所基于的。如果您将其初始化为空客户
,那么Intellisense将检测到它返回客户
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
您也可以使用 ///< param />
指定参数类型:
$.ajax({
...
success: function (foundCustomer) {
/// <param name='foundCustomer' type='Customer' />
foundCustomer. //Customer members appear
}
});
最后,您的文件.URL.length
技巧也可用于强制转换方法:
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;
}
这篇关于创建适用于intellisense的javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!