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 3977364
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:54:53+00:00 2026-05-20T04:54:53+00:00

Would the following make the objects fulfil all characteristics that enums have in JavaScript?

  • 0

Would the following make the objects fulfil all characteristics that enums have in JavaScript? Something like:

my.namespace.ColorEnum = {
  RED : 0,
  GREEN : 1,
  BLUE : 2
}

// later on

if(currentColor == my.namespace.ColorEnum.RED) {
  // whatever
}

Or is there some other way I can do this?

  • 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-20T04:54:54+00:00Added an answer on May 20, 2026 at 4:54 am

    Since 1.8.5 it’s possible to seal and freeze the object, so define the above as:

    const DaysEnum = Object.freeze({"monday":1, "tuesday":2, "wednesday":3, ...})
    

    or

    const DaysEnum = {"monday":1, "tuesday":2, "wednesday":3, ...}
    Object.freeze(DaysEnum)
    

    and voila! JS enums.

    However, this doesn’t prevent you from assigning an undesired value to a variable, which is often the main goal of enums:

    let day = DaysEnum.tuesday
    day = 298832342 // goes through without any errors
    

    One way to ensure a stronger degree of type safety (with enums or otherwise) is to use a tool like TypeScript or Flow.

    Quotes aren’t needed but I kept them for consistency.

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

Sidebar

Related Questions

I would like to make a cycle over the following elements: [1,2,11,12,21,22,111,112,121,122,....,222222] or for
For the following example: http://developer.yahoo.com/yui/examples/tabview/frommarkup_clean.html I would like to make the tabs right aligned
I wanted to make an associative array in Javascript in which I would have
Let's say I have the following objects: Fxy.commands={ group:{ add:function(msg){ So, of course, Fxy.commands.group.add('something')
I have a Java application and I would like to make it extensible. To
I have a DICOM dictionary that contains a set of objects all deriving from
Would the following SQL remove also the index - or does it have to
In my code, I have the following objects to implement a shopping cart: public
Using the Entity Framework Code First paradigm I have defined the following objects and
I have been trying to find ways to make the following piece of code

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.