Tuesday, May 6, 2008

VS 2008 JavaScript Intellisense

VS 2008 JavaScript Intellisense

One of the features that web developers will really like with VS 2008 is its built-in support for JavaScript intellisense. This is enabled in both the free Visual Web Developer 2008 Express edition as well as in Visual Studio, and makes using JavaScript and building AJAX applications significantly easier.
Below is a quick tour of some of the new JavaScript intellisense features to take advantage of:
JavaScript Type Inference
One of the things you'll notice immediately when you start typing within a script block is the richer support that Visual Studio 2008 now has for JavaScript keywords and language features:


JavaScript is a dynamic language, and doesn't support explicit type declarations, which has made implementing good intellisense difficult in the past.
Visual Studio 2008 adds support for type inference, which means that it evaluates and computes how a JavaScript block is being used and dynamically infers the variable usage and type information of the code to provide accurate intellisense support.
For example, Visual Studio below will infer that an html element is being retrieved by the document.getElementById() method, and provide appropriate html element intellisense for the variable result:

If I later assign a numeric value to the "myElement" variable (which in JavaScript converts it to a number), notice how VS will detect this and now provide integer intellisense for the variable later in the method:

Intellisense for External JavaScript Libraries
VS 2008 supports intellisense not just for in-line script, but also for externally referenced JavaScript files. For example, assume we have a "getMessage" function like below defined within a "Util.js" javascript file:


I can then simply add a standard JavaScript script refrence element to my page, and I will then automatically receive intellisense support for it as I code:

Notice how VS automatically provides basic parameter intellisense information on the method without us having to do anything special to the JavaScript for it to appear:


Adding Intellisense Hints to JavaScript
As you saw above, Visual Studio will automatically provide basic intellisense help information for the method name and parameters of standard JavaScript.
You can optionally make this intellisense richer by adding comments to your JavaScript code that the intellisense engine can then pick up and use when you consume a method or library. For example, I could add the below comments to the getMessage function in my util.js file:



And when I then code against it within my "Default.aspx" file Visual Studio will automatically display this summary information for the method:

As well as the parameter details


______

No comments: