views:

517

answers:

1

Have you guys and gals got any tips or hacks for making the most out of the JavaScript intellisense options in Visual Studio 2008?

Consider the following:

var Persons = {};
    Persons.Females = {};

Persons.Females.Julie = function (_mood)
{
    /// <param name="_mood">Mood of Julie</param>
    /// <summary>Constructor function: Julie, a 22 year old female</summary>
    /// <returns>New instance of Julie</returns>

    var breasts, thighs, stomach; // Private variables

    this.mood = _mood; // Public variable


    function accessBodypart(_bodypart) // Private function
    {
        /// <param name="_bodypart">Bodypart to access</param>
    }


    this.access = function (_bodypart, _accessee) // Privileged function
    {
        /// <param name="_bodypart">Access a bodypart on Julie</param>
        /// <param name="_accessee">Person accessing Julie</param>
        /// <summary>If you have sufficient rights, you may use this
        /// function</summary>
        /// <returns>Julie's reaction</returns>

        if (_accessee.status === "boyfriend")
        {
            accessBodypart(_bodypart);

            return "Giggles";
        }

        return "Slap in the face";
    };
};


var happyJulie = Persons.Females.Julie("happy");

Visual Studio shows me the "namespaces" and uses the documentation features (<param> and <summary>). I have not been able to get the <return> documentation feature to work though.

Now, that's all well and good. But when I do:

happyJulie.access("breasts");

Visual Studio does not know about the access function and I get no documentation on it.

Is there any way I can expose public variables and privileged functions to Visual Studios intellisense functionality, while still creating objects with private members?

Yes, using the whole car->wheels->tires gets old at some point :)

+3  A: 

Javascript Intellisense is definitely flaky as far as recognizing function members. I've had slightly more success using the prototype paradigm, so that's something you could check out. Often times, though, I find it still won't reliably list functions in the Intellisense.
Edit: As the original poster suggested in the comments below, it's not really possible to get the same "private" functionality in the prototype model. Javascript doesn't have a concept of private members, but you can emulate member privacy with closure by declaring them in the function constructor. The means though that if you have functions that need to access members, they have to be in the constructor too, so they can't be prototypes.
So while using the prototype model may (or may not) give you better VS Intellisense, it's only useful for public functions that hit public members, and can't be used to improve intellisense for private or privileged functions. Private functions you probably don't want intellisense anyway, but privileged you likely would.

Grank
Correct me if I'm wrong, but that pattern doesn't allow for private members. Or does it? If so; could you give me an example?
roosteronacid
Javascript itself doesn't really allow for private members. You can emulate member privacy through closure, which means putting private members in the constructor, which means functions needing to access them can't be prototypes. So the short answer is "sortof".
Grank
That's what I thought. So the short answer is "no" then? :)
roosteronacid
If we agree; could you edit your post to reflect what your comment. That way I can mark this question resolved.
roosteronacid
Using an underscore _var for private member has worked for me all along. True that it wouldn't keep some other class from editing it, but it's well documented that you shouldn't.
Juan Mendes