jaloplo
jaloplo

Reputation: 951

How can include a dot character in enum type name in javascript?

I'm using javascript and have this enumeration:

filterType = { Campaign : 'Campaign', Class : 'Class', Date : 'Date', 
               DateGeq : 'DateGeq', DateLeq : 'DateLeq', 
               DateRange : 'DateRange', Status : 'Status' }

I'd like to name it as:

Filter.filterType = { Campaign : 'Campaign', Class : 'Class', Date : 'Date', 
                      DateGeq : 'DateGeq', DateLeq : 'DateLeq', 
                      DateRange : 'DateRange', Status : 'Status' }

The interpreter doesn't like dot character.

Can I add a dot character in enumeration names???

Thanks!!!

Upvotes: 0

Views: 763

Answers (4)

Justin Johnson
Justin Johnson

Reputation: 31300

There are no enumerations in JavaScript. What you have shown here is an object, more specifically, an object literal constructed using JSON notation.

You're second example is attempting to create a filterType property (which is a redundant name, by the way) on an object named Filter. If Filter doesn't exist, it will cause an error (consider it analogous to null.filterType which obviously doesn't make any sense). You must first define Filter.

To define Filter and Filter.filterType in one expression, you can use the following notation:

var Filter = {
    filterType: {
        Campaign : 'Campaign', Class : 'Class', Date : 'Date', 
        DateGeq : 'DateGeq', DateLeq : 'DateLeq', 
        DateRange : 'DateRange', Status : 'Status'
    }
};

Upvotes: 0

Dennis C
Dennis C

Reputation: 24747

I guess you have "Filter" is undefined.

var Filter ={};
Filter.filterType = {....}

Upvotes: 2

YOU
YOU

Reputation: 123841

How about doing like this?

Filter={}

Filter.filterType = { Campaign : 'Campaign', Class : 'Class', Date : 'Date', 
                      DateGeq : 'DateGeq', DateLeq : 'DateLeq', 
                      DateRange : 'DateRange', Status : 'Status' }

Upvotes: 1

David Hedlund
David Hedlund

Reputation: 129792

You're probably getting an error because you're trying to assign a value to the filterType member on a class called Filter, but Filter is undefined. It'll work if you defined Filter first.

var Filter = {};

To do it all in one line you could write:

var Filter = { filterType: { ... } };

Upvotes: 5

Related Questions