Is it possible to declare constants to be used in an array in JavaScript? I tried this but I get an error saying that Undefined Reference for the constant values in the array.
Here is my original code:
module.exports = {
ADVICE: 'advice',
APPROVE_ANY: 'approve_any',
APPROVE_FIRST: 'first_reply',
APPROVE_MAJORITY: 'approve_majority',
APPROVE_UNANIMOUS: 'approve_unanimous',
OBJECTION: 'objection',
GRIPE: 'gripe',
TELL: 'tell',
SILENT: 'silent',
DIALOG_TYPES: ['advice', 'approve_any', 'first_reply', 'approve_majority', 'approve_unanimous', 'objection', 'gripe', 'tell', 'silent'],
YES: 'yes',
NO: 'no',
NO_REPLY: 'no_reply',
RESPONSE_TYPES: ['yes', 'no', 'no_reply'],
MOBILE_PROVIDER: ['AT&T', 'T-Mobile', 'Verizon']
}
Here is my code snippet (refactored):
module.exports = {
ADVICE: 'advice',
APPROVE_ANY: 'approve_any',
APPROVE_FIRST: 'first_reply',
APPROVE_MAJORITY: 'approve_majority',
APPROVE_UNANIMOUS: 'approve_unanimous',
OBJECTION: 'objection',
GRIPE: 'gripe',
TELL: 'tell',
SILENT: 'silent',
DIALOG_TYPES: [ADVICE, APPROVE_ANY, APPROVE_FIRST, APPROVE_MAJORITY, APPROVE_UNANIMOUS, OBJECTION, GRIPE, TELL, SILENT],
YES: 'yes',
NO: 'no',
NO_REPLY: 'no_reply',
RESPONSE_TYPES: [YES, NO, NO_REPLY],
MOBILE_PROVIDER: ['AT&T', 'T-Mobile', 'Verizon']
}
DIALOG_TYPESreally wants to be a some kind of key/value mapping, not a straight array. There is nothing in your code to tell you what value DIALOG_TYPES[3] represents right now, other than "I just know", and that makes for imminent bugs later on.