Javascript module trace and memory performance

I am using the Javascript module template to try and implement C # type enumeration functions. I have two ways that I am currently implementing this functionality, but I do not understand all the advantages or advantages of one method compared to another.

Here is implementation 1:

var MyApp = (function (app) {

    // Private Variable
    var enums = {
        ActionStatus: {
            New: 1,
            Open: 2,
            Closed: 3
        }
    };

    // Public Method
    app.getEnum = function (path) {
        var value = enums;            
        var properties = path.split('.');
        for (var i = 0, len = properties.length; i < len; ++i) {
            value = value[properties[i]];
        }
        return value;
    };

    return app;

})(MyApp || {});

// Example usage
var status = MyApp.getEnum("ActionStatus.Open");

And now implementation 2:

var MyApp = (function (app) {

    // Public Property
    app.Enums = {
        ActionStatus: {
            New: 1,
            Open: 2,
            Closed: 3
        }
    };

    return app;

})(MyApp || {});

// Example usage
var status = MyApp.Enums.ActionStatus.Open;

The main difference is the use of the variable "private" vs "public" to store enumerations. I think implementation 1 is a bit slower, but I was not sure that keeping enums as "private" reduced memory usage. Can someone explain the difference in memory and performance for the two (if any)? Any other suggestions / recommendations are appreciated.

+5
1

... , "private"

, -: enums, .

, . (I , forEach , IE6 JS-, ).

, , : Enums, , ECMAScript5 Object.defineProperties:

var Enums = Object.defineProperties({}, {
    ActionStatus: {
        value: Object.defineProperties({}, {
            New:    {value: 1},
            Open:   {value: 2},
            Closed: {value: 3}
        })
    }
});

// Usage
var n = Enums.ActionStatus.New; // 1

, defineProperties, .

, , "" ES5 Object.defineProperties , . "shimmed" , , , , , ( - ), , , .

, , EMCAScript6 , .

+4

All Articles