I'll bench it, about size this is 893 VS 914 ... not a big deal but scope
lookup is easier in my version.

Please consider this.enum break all minifiers since enum is a reserved word,
I had to do this["enum"] = {} at the end indeed.

Look if you find some hint here

(function (Object, exports) {

    /**
     * A simple enum implementation for JavaScript
     * @link        http://www.2ality.com/2011/10/enums.html
     * @author      @rauschma
     * @revision    @WebReflection
     * @note        enum is reserved word. Use var enm = this.enum or var
enm = require("enum");
     */

    var // common shortcut
        EnumPrototype = Enum.prototype,
        defineProperty = Object.defineProperty,
        forEach = [].forEach,
        freeze = Object.freeze,
        getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor,
        getOwnPropertyNames = Object.getOwnPropertyNames,
        keys = Object.keys
    ;

    // module constructors
    function Enum(obj) {
        if (arguments.length == 1 && obj != null && typeof obj == "object")
{
            forEach.call(keys(obj),
                assignSymbolProperties,
                {s: this, t: obj}
            );
        } else {
            forEach.call(arguments, assignSymbol, this);
        }
        freeze(this);
    }

    function Symbol(name, props) {
        this.name = name;
        props && copyOwnTo(props, this);
        freeze(this);
    }

    // helpers
    function assignSymbol(name) {
        this[name] = new Symbol(name);
    }

    function assignSymbolProperties(name) {
        this.s[name] = new Symbol(name, this.t[name]);
    }

    function copyOwnTo(source, target) {
        var names = getOwnPropertyNames(source);
        names && forEach.call(names,
            copyEachProperty,
            {s: source, t: target}
        );
        return target;
    }

    function copyEachProperty(propName) {
        defineProperty(
            this.t,
            propName,
            getOwnPropertyDescriptor(
                this.s,
                propName
            )
        );
    }

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

    // secure Symbol prototype
    freeze(
        Symbol.prototype = Object.create(null, {
            constructor: {
                value: Symbol
            },
            toString: {
                value: function toString() {
                    return "|"+this.name+"|";
                }
            }
        })
    );

    // extend Enum prototype
    defineProperty(EnumPrototype, "contains", {
        enumerable: true,
        value: function contains(sym) {
            return sym instanceof Symbol ?
                this[sym.name] === sym :
                false
            ;
        }
    });
    defineProperty(EnumPrototype, "symbols", {
        enumerable: true,
        value: function symbols() {
            return keys(this).map(mapSymbolValue, this);
        }
    });

    // exports
    exports.Enum = Enum;
    exports.Symbol = Symbol;

}(Object, typeof exports == "undefined" ? this["enum"] = {} : exports));

Best Regards,
    Andrea Giammarchi

On Mon, Oct 24, 2011 at 11:53 PM, Axel Rauschmayer <[email protected]> wrote:

> If you don't want Object.prototype (why ?)
> do you want an enumerable, configurable, writable constructor ?
>
>
> I don’t care much either way. The main point for me is that symbol
> instances are immutable. If all of the instance’s prototype are also
> immutable, the instance itself is "more immutable". Again, I don’t have
> strong feelings for that.
>
> Why not just this ?
> exports.Symbol.prototype = Object.create(null, {constructor: {value: 
> exports.Symbol}});
>
>
> I don’t see much difference with my solution, either regarding conciseness
> or regarding performance.
>
> Also why so many runtime closures and "var that = this" ?
>
>
> Runtime closures:
> - Does performance really matter here? I find my solution easier to read.
> - Note that binding "this" via forEach() and map() also has performance
> implications.
>
> that = this:
> - Your point is that forEach() and map() have a second parameter that
> allows you to hand in a value for "this", right?
>    => I have yet to find a clear argument in favor or against using that =
> this here. Both solutions have performance implications, both solutions add
> an extra line. that = this has two extra tokens, but adding a second
> argument looks awkward.
>
> 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.
>
>
> Yes, good idea, it’ll make things easier to read.
>
> --
> Dr. Axel Rauschmayer
> [email protected]
>
> home: rauschma.de
> twitter: twitter.com/rauschma
> blog: 2ality.com
>
>
>
>
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to