gpt4 book ai didi

javascript - 创建适用于智能感知的 javascript 函数

转载 作者:行者123 更新时间:2023-11-29 14:54:58 24 4
gpt4 key购买 nike

我想利用 visual studio intellisense,因此我阅读了:

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

无论如何,为什么我没有得到 intellisense:

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 知道该函数将返回类型为 Customer 的对象?例如,如果我将函数 Test 替换为: function Test(){ return new Customer(); } 然后智能将工作。


编辑

我最后的目标是:

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 对象,我想使用这个辅助函数来转换它们。


临时解决方案:

这就是我最终做的:

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!!!!
}
});

最佳答案

您正在将返回值初始化为 Object所以这是 Intellisense 所基于的。如果将其初始化为空 Customer ,然后 Intellisense 将检测到它正在返回一个 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
}
});

最后,你的 document.URL.length技巧也可以用在类型转换方法中:

Object.prototype.CastToCustomer = function() {

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

}

关于javascript - 创建适用于智能感知的 javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19430321/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com