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 <[email protected]> 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
[email protected]
twitter.com/rauschma

Home: rauschma.de
Blog: 2ality.com

_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to