Enums in Javascript

TL;DR: Enums in Javascript

Define your enum like so:

var SizeEnum = {
  SMALL: 1,
  MEDIUM: 2,
  LARGE: 3,
};

Then use it like so:

var mySize = SizeEnum.SMALL;

If you want the enum values to hold properties, you can add them to an extra object:

var SizeEnum = {
  SMALL: 1,
  MEDIUM: 2,
  LARGE: 3,
  properties: {
    1: {name: "small", value: 1, code: "S"},
    2: {name: "medium", value: 2, code: "M"},
    3: {name: "large", value: 3, code: "L"}
  }
};

Then use it like so:

var mySize = SizeEnum.MEDIUM;
var myCode = SizeEnum.properties[mySize].code; // myCode == "M"


Background information

The format described above is the result of quite some time of thinking about enums in Javascript. It tries to combine the best of both worlds of using primitives as the enum values (safe for de/serializing) and using objects as the values (allows properties on the values). Read further to learn how I came to this format.

Rediscovering enums

I recently stumbled onto a question on StackOverflow that I had answered myself a couple of years back and did some more thinking about it after reading some of the comments and decided that this topic was worth an article.

So what was the question I hear you ask?

What is the best way to write enums in Javascript?

First of all, before answering this question, we have to have a look at what an enum is and what it means to write one in Javascript. So let us look at a definition of enum:

What is an enum?

In computer programming, an enumerated type (also called enumeration or enum [..]) is a data type consisting of a set of named values called elements, members or enumerators of the type. The enumerator names are usually identifiers that behave as constants in the language. A variable that has been declared as having an enumerated type can be assigned any of the enumerators as a value.
Wikipedia: Enumerated type

And a good example often beats a formal definition:

enum WeekDay = {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY};

So, summarizing: An enum is a type restricting variables to one value from a predefined set of constants. In the example above, WeekDay is an enum and MONDAY, TUESDAY etc are the constants in the set, also called the enumerators. If we declare a variable as

WeekDay payDay;

..we would be able to assign it any of the constants MONDAY, TUESDAY etc, up to and including SUNDAY, but not something else like 12 or "labour day".

…which brings us to a problem.

Continue reading

Apache server 2.4.6 hangs after serving requests from Internet Explorer 10/11

Symptoms:
Apache 2.4 running on Windows hangs when Internet Explorer 10/11 is used to acces pages on it.

apache-feather-logo-370x229

Reproduction:
I had a very reproducible scenario: Only the first request to Apache coming from IE would work, all other subsequent requests would hang, in all browsers. As long as I did not use Internet Explorer there did not seem to be a problem, but once that was used everything would hang until Apache was restarted. Strangely enough after I had applied the fix (see below) I reverted it to do some more testing but was not able to reproduce the problem anymore…

Continue reading