Enums?

Andrea Giammarchi andrea.giammarchi at gmail.com
Mon Oct 24 14:25:43 PDT 2011


/** We don’t want the mutable Object.prototype in the prototype chain */
    exports.Symbol.prototype = Object.create(null);
    exports.Symbol.prototype.constructor = exports.Symbol;

If you don't want Object.prototype (why ?)
do you want an enumerable, configurable, writable constructor ?

Why not just this ?
exports.Symbol.prototype = Object.create(null, {constructor: {value:
exports.Symbol}});

Also why so many runtime closures and "var that = this" ?

function recycledF1(name) {
    this.s[name] = new exports.Symbol(name, this.o[name]);
}

function recycledF2(name) {
    this[name] = new exports.Symbol(name);
}

function recycledF3(key) {
    return this[key];
}

exports.Enum = function (obj) {
    if (
        arguments.length === 1 &&
        // (obj !== null && typeof obj === "object") // 2 checks for undefined
        obj != null && typeof obj === "object"
    ) {
        Object.keys(obj).forEach(recycledF1, {s:this, o:obj});
    } else {
        [].forEach.call(arguments, recycledF2, this);
    }
    Object.freeze(this);
}

exports.Enum.prototype.symbols = function() {
    return Object.keys(this).map(recycledF3, this);
}

I would also drop all these anonymous functions using the closure with
declarations for Symbol and Enum and export once at the end rather
than reference the exports object each time.

This is because I like the idea *and* I would like to make it as small
as possible once minified and as memory/performances improved as
possible for mobile.

What do you think ?

Best Regards,
    Andrea Giammarchi





On Mon, Oct 24, 2011 at 8:42 PM, Axel Rauschmayer <axel at rauschma.de> wrote:

> FWIW: I’ve blogged about this.
> http://www.2ality.com/2011/10/enums.html
>
> On Oct 3, 2011, at 19:03 , Allen Wirfs-Brock wrote:
>
> 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 <axel at 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
> axel at rauschma.de
> twitter.com/rauschma
>
> Home: rauschma.de
> Blog: 2ality.com
>
>
>
> --
> Dr. Axel Rauschmayer
> axel at rauschma.de
>
> home: rauschma.de
> twitter: twitter.com/rauschma
> blog: 2ality.com
>
>
>
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20111024/fbe061c3/attachment-0001.html>


More information about the es-discuss mailing list