创建使用intellisense的javascript函数
我想利用visual studio intellisense,因此我阅读了: 不管怎样,为什么我不能通过以下方式获得智能感知:创建使用intellisense的javascript函数,javascript,visual-studio,intellisense,Javascript,Visual Studio,Intellisense,我想利用visual studio intellisense,因此我阅读了: 不管怎样,为什么我不能通过以下方式获得智能感知: function Customer() { this.Id = 0; this.FirstName = ""; this.LastName = ""; } function Test() { /// <returns type="Customer"></returns> var c = new Obj
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?
我得到了很多json对象,我想使用这个helper函数来转换它们
临时解决办法: 这就是我最后做的:
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!!!!
}
});
如果将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=foundCustomer
更改为c=newcustomer(foundCustomer)
。我猜这可能会触发intellicense?您正在初始化对象的返回值,因此它是Intellisense所基于的。如果将其初始化为空的客户
,则Intellisense将检测到它正在返回客户
function Test() {
var c = new Customer(); //only for Intellisense
$.ajax({...});
return c;
}
Test(). //Customer members now appear
您还可以使用//
指定参数类型:
$.ajax({
...
success: function (foundCustomer) {
/// <param name='foundCustomer' type='Customer' />
foundCustomer. //Customer members appear
}
});
您希望Intellisense在这里说什么,您还没有在类型Customer
本身中添加任何内容?Id、FirstName和LastName。。。与我在何处操作相同的智能:var x=new Customer();x、 //智能许可证将起作用代码>是的,我喜欢如果(document.URL.length)c=this代码>我将在制作演员阵容之前使用它,避免编写类似以下内容:/
$.ajax({
...
success: function (foundCustomer) {
/// <param name='foundCustomer' type='Customer' />
foundCustomer. //Customer members appear
}
});
Object.prototype.CastToCustomer = function() {
var c = new Customer();
if (document.URL.length) c = this;
return c;
}