-1

I was interested in a really quick enumeration of constant values for a project I'm working on, but everything I found on StackOverflow was ridiculously over-complicated if all you want is to store several unchanging values in a single place so that you can reuse them. They also used objects, which meant you could change their values, or you had to understand and use a "freeze" keyword which may not be implemented in your browser.

Is the obvious use of closures here a bad idea? Maybe I'm using them too much, but it seems like they come in handy everywhere. Especially here.

var black = new Color3f(0, 0, 0);
var white = new Color3f(1, 1, 1);
var blue = new Color3f(0, 0, 1);

var Colors = {

    //insert basic stuff for Color3f objects here

    BLACK: function(){
        return black;
    },

    //and so on
};
starblue
  • 51,675
  • 14
  • 88
  • 146
  • What do you mean by "viable implementation"? JS doesn't support enumerations the way that strongly typed languages do. However, since every object is a hashtable in its own right, you can certainly make lookup objects, but you can't prevent someone from modifying said lookup object. – zzzzBov Sep 21 '12 at 17:53
  • not really an enumeration which implies order, this is just a bunch of 'constants' - http://stackoverflow.com/a/131286/1097 – Geoff Sep 21 '12 at 17:55
  • By "viable" I mean "as useful as any of the other implementations I saw when looking at other questions". However, if my understand of enumeration is wrong, as Geoff implies, then that would explain why the other answers were more complicated than just packing the values in closures. – asdfStackExchange Sep 21 '12 at 17:58

2 Answers2

1

Objects can't create closure in JavaScript because they never create variable scope.

To use closure, you'd do something like this.

var color = (function() {

    var color_map = {
        black: new Color3f(0, 0, 0),
        white: new Color3f(1, 1, 1),
        blue: new Color3f(0, 0, 1)
    };

    return function(c) { return color_map[c.toLowerCase()]; };
})();

Then invoke the color function to fetch a color.

color("black");

Take note that the outer function is invoked immediately, and the inner function is the one that is assigned to the color variable.

Also, you may want to wrap all your code in another function to prevent any sort of modification.

(function() {
    var color = (function() {
        var color_map = {
            black: new Color3f(0, 0, 0),
            white: new Color3f(1, 1, 1),
            blue: new Color3f(0, 0, 1)
        };

        return function(c) { return color_map[c.toLowerCase()]; };
    })();

    // the rest of your code
    alert(color("black"));
})();
scatter
  • 49
  • 1
0

No need for the extra global variables:

var Colors = {

    //insert basic stuff for Color3f objects here

    BLACK: new Color3f(0, 0, 0),

    //and so on
};
jbabey
  • 42,963
  • 11
  • 66
  • 94
  • Then anyone can do this: Colors.BLACK = new Color3f(1, 1, 1); ... var someMaterial.color = Colors.BLACK; Are we not worried by the fact that our enums are mutable? – asdfStackExchange Sep 21 '12 at 17:54
  • @asdfStackExchange anyone can do this: `var undefined = true;` but we don't really write hacks to protect against it - it's all about cost/benefit. – jbabey Sep 21 '12 at 17:57
  • @asdfStackExchange you've done your due diligence to put your "enum" in a "namespace" (though `Colors` is probably not a great name), that's about as much as you can do. – jbabey Sep 21 '12 at 18:00