Writing for JScript Intellisense in VS 2008

Since we release JSript intellisense in Visual Studio 2008 Beta 2, there has been some queries on why JScript intellisense behaves in a certain way. I wish to tell you that intellisense in a dynamic language like JScript is based on certain heuristics. We try our best to guess the runtime behavior and provide what we think is the best guess. As such, it is not always what a user might expect.

To illustrate, consider the following example:

  1: Person = function() {
 2:     this.name = “Anonymous”;
 3: }
 4:
 5: Person.prototype = {
 6:     getName : function() {
 7:         return this.name;               // I don’t see “name” on the list
 8:     }
 9: }

When we type “this.” In line 7, we don’t see “name” in the intellisense dropdown that pops up. This is contrary to what a user might expect. But consider the following usage:

 10: Person.prototype.getName();

In this case, we get an undefined value since “name” is not defined in this context. So, it is not always the case that “name” would be defined. To avoid such ambiguity, the above can be modified so that all variables are defined in the prototype:

 11: Person = function() { }
12:
13: Person.prototype = {
14:     name : “Anonymous”,
15:     getName : function() {
16:         return this.name;               // I see “name” on the list now
17:     }
18: }

Now, you can see “name” in the intellisense drop down.

Update 11/12/2007: There have been some recent comments on this post which rightly point out that there is a functionality difference between the two. The difference is subtle. And, if you are unsure of the functionality difference , I would suggest that you start by reading this post on Classes in Jscript by Ritesh.

 

Sameer