Welcome to MSDN Blogs Sign in | Join | Help

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

Published Thursday, November 08, 2007 6:15 AM by Sameerch

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# MSDN Blog Postings » Writing for JScript Intellisense in VS 2008

# re: Writing for JScript Intellisense in VS 2008

Thursday, November 08, 2007 10:54 AM by Kevin Blakeley

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.

# re: Writing for JScript Intellisense in VS 2008

Thursday, November 08, 2007 3:10 PM by Speednet

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!

# re: Writing for JScript Intellisense in VS 2008

Thursday, November 08, 2007 4:39 PM by Ptah

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.

# re: Writing for JScript Intellisense in VS 2008

Sunday, November 11, 2007 8:00 AM by Luis Abreu

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

# Getting intellisense in JS code

Sunday, November 11, 2007 5:50 PM by LA.NET [EN]

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

# re: Writing for JScript Intellisense in VS 2008

Sunday, November 11, 2007 11:47 PM by Sameerch

Kevin,

Yes, you are right.  There was a bug in VS 2008 Beta 2.  This should be resolved in the next release.

Sameer

# re: Writing for JScript Intellisense in VS 2008

Monday, November 12, 2007 12:18 AM by Sameerch

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.

# re: Writing for JScript Intellisense in VS 2008

Monday, November 12, 2007 1:09 AM by bleroy

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

# re: Writing for JScript Intellisense in VS 2008

Monday, November 12, 2007 1:18 AM by Ptah

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).

# re: Writing for JScript Intellisense in VS 2008

Saturday, January 19, 2008 7:58 AM by Veri

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).

# re: Writing for JScript Intellisense in VS 2008

Saturday, January 19, 2008 8:09 AM by Vista

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.

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker