Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
创建使用intellisense的javascript函数_Javascript_Visual Studio_Intellisense - Fatal编程技术网

创建使用intellisense的javascript函数

创建使用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

我想利用visual studio 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?
我得到了很多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;

}