最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Let Visual Studio 2010 JavaScript IntelliSense know the type of object - Stack Overflow

programmeradmin4浏览0评论

Let's say I have the javascript function below:

  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. If not, the msg is a string, and I print the variable itself. 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?

Let's say I have the javascript function below:

  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. If not, the msg is a string, and I print the variable itself. 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?

Share Improve this question asked Dec 18, 2010 at 20:43 PascalPascal 2,9848 gold badges52 silver badges79 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 10

Actually it's not limited to local variables. You can help VS by using xml ments like this:

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 prehensive enough.

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 pound 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 pound 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. So, it seems that Intellisense is pretty limited to local variables.

发布评论

评论列表(0)

  1. 暂无评论