Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7544743
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:45:08+00:00 2026-05-30T08:45:08+00:00

I am using the Javascript Module Pattern to try and implement C# enumeration-like functionality.

  • 0

I am using the Javascript Module Pattern to try and implement C# enumeration-like functionality. I have two ways that I am currently thinking about implementing this functionality but I do not understand all the benefits or advantages of one way versus the other.

Here is implementation 1:

var MyApp = (function (app) {

    // Private Variable
    var enums = {
        ActionStatus: {
            New: 1,
            Open: 2,
            Closed: 3
        }
    };

    // Public Method
    app.getEnum = function (path) {
        var value = enums;            
        var properties = path.split('.');
        for (var i = 0, len = properties.length; i < len; ++i) {
            value = value[properties[i]];
        }
        return value;
    };

    return app;

})(MyApp || {});

// Example usage
var status = MyApp.getEnum("ActionStatus.Open");

And now implementation 2:

var MyApp = (function (app) {

    // Public Property
    app.Enums = {
        ActionStatus: {
            New: 1,
            Open: 2,
            Closed: 3
        }
    };

    return app;

})(MyApp || {});

// Example usage
var status = MyApp.Enums.ActionStatus.Open;

The main difference is in using a “private” variable vs a “public” property to store the enums. I would think implementation 1 is a little slower but I was not sure if keeping the enums as “private” reduced the memory usage. Can anyone explain the difference in memory footprint and performance for the two (if any)? Any other suggestions/advice are appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:45:09+00:00Added an answer on May 30, 2026 at 8:45 am

    …but I was not sure if keeping the enums as “private” reduced the memory usage

    The opposite, if anything: You still have to have the enums object, and you have to have a function to access it.

    In terms of speed, I wouldn’t worry about it. The added function call won’t make any real difference (I looked into it when worried about using the new forEach and such, and even on IE6 with its massively slow JS engine, it just doesn’t matter).

    In a couple of years, you’ll probably be able to have the best of both worlds: Enums that are read-only, thanks to ECMAScript5’s Object.defineProperties feature:

    var Enums = Object.defineProperties({}, {
        ActionStatus: {
            value: Object.defineProperties({}, {
                New:    {value: 1},
                Open:   {value: 2},
                Closed: {value: 3}
            })
        }
    });
    
    // Usage
    var n = Enums.ActionStatus.New; // 1
    

    By default, properties created with defineProperties are read-only.

    In fact, you can basically have that now if you add an ES5 “shim” to create Object.defineProperties on browsers that don’t yet have it natively. The “shimmed” version would create read-write properties, since only the natively-supported version can really create read-only properties, but you can write the code now and know that it will work as you like on modern browsers (about half of all web surfers currently have them) while still working, just with less robustness, on less-modern ones.

    And of course, EMCAScript6 may take things further, but that’s still a future thing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have defined a GWT module that includes an external javascript file using tag.
When using the module pattern in javascript how should constructors be defined, if at
In OO Javascript constructor pattern: neo-classical vs prototypal , I learned that constructors using
I've been using javascript module pattern for while. I showed an example of a
I've been writing some pseudo-oop JavaScript using the Module pattern. I'm curious, what is
I'm using the JQUERY UI Accordion module: <script type=text/javascript> $(function() { $(#sidebar_column_accordion).accordion({ fillSpace: true,
Using JavaScript, how do I create an HTML table that can "accept" numeric matrix
Using Javascript, I would like change the background color of the numbers in an
I'm using the module pattern , one of the things I want to do
I have a file uploader module. The UI is created using simple HTML and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.