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

Comments

  • Anonymous
    November 07, 2007
    PingBack from http://msdnrss.thecoderblogs.com/2007/11/08/writing-for-jscript-intellisense-in-vs-2008/

  • Anonymous
    November 08, 2007
    Should we see it work this way in Beta 2 or were there bug fixes made to get this to work that we will see in the final version?  I tried your example and still could not get the intellisense to pick it up. I am having simliar properties when I try to access other methods defined in my class.  As I type this.get_SomeVariable inside the initialize method of my class, the "get_SomeVariable" does not show up in the list either.

  • Anonymous
    November 08, 2007
    I hope everyone reading this doesn't go and put all their property definitions in the prototype, just because of IntelliSense.  There is a huge functionality difference between putting something in the constructor vs. putting it in the prototype!

  • Anonymous
    November 08, 2007
    I agree with SpeedNet. This kind of thing is going to encourage people to put things in a way just so that Intellisense might pick it up (read: because they're likely too lazy to read/be familiar with the code at hand), regardless of the runtime ramifications (which, given their presumed laziness, they're likely not to appreciate anyhow, except by incidental, dismayed surprise). If I were Microsoft, I'd steer entirely clear of trying to add Intellisense to a dynamic langauge like JScript/JavaScript -- you're just opening yourself up to complaints and attacks from those who misunderstand/don't bother to understand what's really going on and why. Look, I'm not advocating that we all get back to Notepad (although it has it's appeal), but in cases like this, the "powertool" IDE just gets in the way for the experienced, and misleads the inexperienced.

  • Anonymous
    November 11, 2007
    hello. well, i agree. this is completly wrong. I won't add a "shared" property on  my js classes just so that it gets shown in intellisense! wrong, wrong and wrong...

  • Anonymous
    November 11, 2007
    Today, I've noticed an entry on the JScript blog that gave some advice on how to get intellisense

  • Anonymous
    November 11, 2007
    Kevin, Yes, you are right.  There was a bug in VS 2008 Beta 2.  This should be resolved in the next release. Sameer

  • Anonymous
    November 11, 2007
    Speednet, yes, I agree.  There is a functionality difference between putting something in the constructor and putting it in the prototype.  But, where it fits, this is one way of re-writing your code so that you get a better intellisense. Ptah, that is an interesting viewpoint you have there.

  • Anonymous
    November 11, 2007
    In the case of a string property, it's actually a little better to put it in the prototype. You really get in trouble with object or array properties. But just to be consistent and remain simple, it's better to just put all field initialization in the constructor. A better explanation here: http://weblogs.asp.net/bleroy/archive/2006/10/07/Careful-with-that-prototype_2C00_-Eugene.aspx

  • Anonymous
    November 11, 2007
    My last post reads troll-ish. Sorry. Look, my point is that adding Intellisense to a strongly-typed, compiled language makes sense because it has all the context-data(via static-analysis, etc) it needs to do the right thing -- the thing that is truly an aid to the developer's productivity -- much more often than not; in any other situation, where a system like Intellisense (or autocomplete in Word, etc) doesnt have the proper context-data, it's the reverse -- productivity is hindered. The fact is, in a dynamic language like JScript, Intellisense will always be a "leaky abstraction" that is bound to do more harm than good overall (http://en.wikipedia.org/wiki/Leaky_abstraction).

  • Anonymous
    January 18, 2008
    I agree with SpeedNet. This kind of thing is going to encourage people to put things in a way just so that Intellisense might pick it up (read: because they're likely too lazy to read/be familiar with the code at hand), regardless of the runtime ramifications (which, given their presumed laziness, they're likely not to appreciate anyhow, except by incidental, dismayed surprise).

  • Anonymous
    January 19, 2008
    If I were Microsoft, I'd steer entirely clear of trying to add Intellisense to a dynamic langauge like JScript/JavaScript -- you're just opening yourself up to complaints and attacks from those who misunderstand/don't bother to understand what's really going on and why.