Channeling for MarkM, if you use regular objects for the enumeration element values you probably will want to freeze them all so they can't be used as a communications channel.
You may also want to define them with a null [[Prototype]]. I intended to do that for e in my original formulation for it got lost along the way from my mind to my finders. For debugging output you might want to capture the string of each numeration element value and provide a toString method. Allen On Oct 3, 2011, at 2:28 AM, Axel Rauschmayer wrote: > Nice one! > > It doesn’t even have to be Name.create() – new Object() works just fine. And > you can use switch with this pattern (I ES5-ified the code): > > function Enum() { > let e = {}; > Array.prototype.forEach.call(arguments, > function(name) { > e[name] = new Object(); > }); > return Object.freeze(e); > } > > var color = Enum("red", "green", "blue"); > var c = color.green; > switch(c) { > case color.red: > alert("red"); > break; > case color.green: > alert("green"); > break; > } > > > On Oct 3, 2011, at 1:44 , Allen Wirfs-Brock wrote: > >> >> On Sep 30, 2011, at 7:33 PM, Dean Landolt wrote: >> >>> >>> >>> On Fri, Sep 30, 2011 at 10:13 PM, Axel Rauschmayer <a...@rauschma.de> wrote: >>> One language feature from JavaScript that I miss are enums. Would it make >>> sense to have something similar for ECMAScript, e.g. via >>> Lisp-style/Smalltalk-style symbols plus type inference? If yes, has this >>> been discussed already? I feel strange when I simulate symbols with strings. >>> >>> >>> ISTM private name objects make a pretty nice stand-in for symbols. >> >> function Enum(...names) { >> let e = Object.create(); >> names.forEach(function(n) {e[n] = Name.create()}); >> return Object.freeze(e); >> } >> >> ... >> >> const colors = Enum('red,'blue','green',yellow',orange','purple'); >> backgournd = colors.red; >> foreground = colors.blue; >> ... >> >> >> Allen > > -- > Dr. Axel Rauschmayer > a...@rauschma.de > twitter.com/rauschma > > Home: rauschma.de > Blog: 2ality.com >
_______________________________________________ es-discuss mailing list es-discuss@mozilla.org https://mail.mozilla.org/listinfo/es-discuss