简体   繁体   English

让Visual Studio 2010 JavaScript IntelliSense知道对象的类型

[英]Let Visual Studio 2010 JavaScript IntelliSense know the type of object

Let's say I have the javascript function below: 假设我有以下javascript函数:

  function (msg) {
    var divForResult = document.getElementById("test");
    if (typeof (msg) == "object")
    {
      divForResult.innerHTML = "Result: <b>" + msg.Message + "</b>";
    }
    else {
      divForResult.innerHTML = "Result: <b>" + msg + "</b>";
    }
  }

I know that if the msg variable is an object, it's as Exception, so I print the Message property. 我知道如果msg变量是一个对象,它就像Exception一样,所以我打印了Message属性。 If not, the msg is a string, and I print the variable itself. 如果没有,则msg是一个字符串,我自己打印变量。 My question is how do I let Visual Studio 2010 JavaScript IntelliSense "know" the type of object msg is, so that I'll get the correct properties/functions for the object type in a situation such as this? 我的问题是如何让Visual Studio 2010 JavaScript IntelliSense“知道”msg对象的类型,以便在这种情况下我能获得对象类型的正确属性/函数?

Actually it's not limited to local variables. 实际上它并不局限于局部变量。 You can help VS by using xml comments like this: 您可以使用xml注释来帮助VS,如下所示:

function foo(message) {
    /// <param name="message" type="String"></param>
    message. //ctr+space here
}

It is not exactly what you are asking for, but it works great when you are accepting argument of one type only. 它并不完全是你所要求的,但是当你只接受一种类型的论证时它会很有效。

Unfortunately, Visual Studio's " pseudo-execution " of JavaScript in order to provide better Intellisense support is still not comprehensive enough. 不幸的是,Visual Studio的“ 伪执行 ”JavaScript为了提供更好的Intellisense支持仍然不够全面。

For example, I wrote this little function: 例如,我写了这个小函数:

var foo = function(obj) {
  if (typeof obj === "string") {
    // presumably Intellisense should know obj is a string 
    // in this compound statement but it doesn't.
    // try "obj." here
  }

  if ((typeof obj === "object") && (obj.constructor === Date)) {
    // presumably Intellisense should know obj is a Date 
    // in this compound statement but it doesn't.
    // try "obj." here
  }

};

And if you try it out VS2010 doesn't notice that in the two clauses I've tried to limit the type of the passed-in object and that therefore it could provide better suggestions. 如果你试一试VS2010并没有注意到在这两个条款中我试图限制传入对象的类型,因此它可以提供更好的建议。 So, it seems that Intellisense is pretty limited to local variables. 因此,似乎Intellisense非常局限于局部变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM