Looking through the Department of Better Technology's Formbuilder library, I see several cases of strings mapped to capital-letter object properties, as if imitating constants in JavaScript. The following is in main.coffee:
mappings:
SIZE: 'field_options.size'
UNITS: 'field_options.units'
LABEL: 'label'
FIELD_TYPE: 'field_type'
...
These "constants" are then mostly interpolated in strings that make up views. From paragraph.coffee:
view: """
<textarea class='rf-size-<%= rf.get(Formbuilder.options.mappings.SIZE) %>'></textarea>
"""
Is there a performance or architectural advantage in doing this, aside from not having to use literals? Seems like it would be less painstaking, if not faster, just to use the string.
Poor man's declaration safety.
rf.get('field_optons.size')
// => undefined (I assume, depends on rf.get)
rf.get(Formbuilder.optons.mappings.SIZE)
// => TypeError: Cannot read property 'mappings' of undefined
A typo in the first case lets your code proceed with a wrong value (undefined
).
A typo in the second case, unless it is in the last component, will abort your program.