Salient Solutions

wrasslin ones and nones for fun and profit - Sky Sanders' Blog
Get your own ranked flair here
posts - 92, comments - 103, trackbacks - 0

Javascript enum creation pattern with Visual Studio intellisense support

visual studio javascript intellisense friendly 
// enum creation pattern
// NOTE: i am not doing the intensive argument validation
// and restricting the enum values to integer as is done in 
// msajax so you can probably break this with little effort.


function createEnum(type, flags)
{
    for (var i in type.prototype)
    {
        type[i] = type.prototype[i];
    }
    // __xxx props are msajax/vs talking to each other
    type.__enum = true;
    type.__flags = flags;
}

intEnum = function()
{
    /// <summary>
    /// intEnum - standard enum type
    /// </summary>
    /// <field name="add" type="Number" integer="true" static="true" >add</field>
    /// <field name="remove" type="Number" integer="true" static="true">remove</field>
    /// <field name="reset" type="Number" integer="true" static="true" >reset</field>

    throw new Error("constructor not implemented. this is a static enum");
}
intEnum.prototype = {
    add: 0,
    remove: 1,
    reset: 2
}
createEnum(intEnum);



stringEnum = function()
{
    /// <summary>
    /// stringEnum - interesting - a string constant enum.
    ///  something I have always wanted in clr code
    /// </summary>
    /// <field name="add" type="String" static="true" >add</field>
    /// <field name="remove" type="String" static="true">remove</field>
    /// <field name="reset" type="String" static="true" >reset</field>

    throw new Error("constructor not implemented. this is a static enum");
}

stringEnum.prototype = {
    add: "add",
    remove: "remove",
    reset: "reset"
}

createEnum(stringEnum);

// vs intellisense works as it evaluates referenced scripts in the background
// so you will not see the fruits of this labor while in the file
// that the enums are declared in. Go to the html page or to another script that
// has a /// <reference path="xxx.js" /> tag at the top , press ctrl+alt+j to
// update intellisense and bingo -you have enums and pretty intellisense visual
// cues.

Technorati tags: , , ,

Print | posted on Wednesday, July 29, 2009 3:54 AM | Filed Under [ CodeProject-Tip ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 7 and 2 and type the answer here:

Powered by: