// all examples are taken from JavaScript The Definitive Guide 6th Edition
function enumeration(namesToValues) {
// a dumm constructor for whatever reason??
// hm maybe we are a factory stuff
var enumeration = function() { throw "Can't instantiate enumerations"; };
var proto = enumeration.prototype = {
constructor: enumeration,
toString: function() { return this.name; },
valueOf: function() { return this.value; },
toJSON: function() { return this.name; }
};
enumeration.values = [];
for(name in namesToValues) {
var e = Object.create(proto);
e.name = name;
e.value = namesToValues[name];
enumeration[name] = e;
enumeration.values.push(e);
}
enumeration.foreach = function(f,c) {
for(var i=0; i < this.values.length; i++) {
f.call(c, this.values[i]);
}
};
return enumeration;
}
// now we create a 'coin class'?
var Coin = enumeration({ Penny: 1, Nickel: 5, Dime: 10, Quarter: 25 });
console.log(Coin); // we get an error, because we can't instantiate enumerations
var c = Coin.Dime;
console.log(c);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.