15

I want to define a list of constants that have continuous integer value, for example:

var config.type = {"RED": 0, "BLUE" : 1, "YELLO" : 2};  

But it's boring to add a "XX" : y every time I need to add a new element in it.
So I'm wondering is there something like enumerator in C so I can just write:
var config.type = {"RED", "BLUE", "YELLO"} and they are given unique integer value automatically.

1
  • @ngen maybe I didn't make it clear. I'd like to refer to type like this: config.type.RED or config.type.BLUE, not config.type[0] Commented Jun 15, 2011 at 3:36

5 Answers 5

17

You could also try to do something like this:

function Enum(values){
    for( var i = 0; i < values.length; ++i ){
        this[values[i]] = i;
    }
    return this;
}
var config = {};
config.type = new Enum(["RED","GREEN","BLUE"]);
// check it: alert( config.type.RED );

or even using the arguments parameter, you can do away with the array altogether:

function Enum(){
    for( var i = 0; i < arguments.length; ++i ){
        this[arguments[i]] = i;
    }
    return this;
}
var config = {};
config.type = new Enum("RED","GREEN","BLUE");
// check it: alert( config.type.RED );
Sign up to request clarification or add additional context in comments.

Comments

6

Just use an array:

var config.type =  ["RED", "BLUE", "YELLO"];

config.type[0]; //"RED"

3 Comments

This also lets you do config.type.indexOf('BLUE'); // 1 to get the reverse if you already have the colors and need to find the index value.
@daybreaker, that should be true as well
maybe I didn't make it clear. I'd like to refer to type like this: config.type.RED or config.type.BLUE, not config.type[0]
2

Use an array ([]) instead of an object ({}), then flip the array to swap keys/values.

Comments

2

Define the Enum:

var type = {
  RED: 1,
  BLUE: 2, 
  YELLO: 3 
};

get the color:

var myColor = type.BLUE;

Comments

1

I suppose you could make a function that accepts an Array:

function constants( arr ) {

    for( var i = 0, len = arr.length, obj = {}; i < len; i++ ) {
        obj[ arr[i] ] = i;
    }
    return obj;
}


var config.type = constants( ["RED", "BLUE", "YELLO"] );

console.log( config.type );  // {"RED": 0, "BLUE" : 1, "YELLO" : 2}

Or take the same function, and add it to Array.prototype.

Array.prototype.constants = function() {

    for( var i = 0, len = this.length, obj = {}; i < len; i++ ) {
        obj[ this[i] ] = i;
    }
    return obj;
}


var config.type = ["RED", "BLUE", "YELLO"].constants();

console.log( config.type );  // {"RED": 0, "BLUE" : 1, "YELLO" : 2}

1 Comment

can't you do that 1st function using the arguments variable of the function and passing in an indeterminate amount of parameters instead of an array?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.