I want to use string constants instead of direct strings across my Javascript objects (when defining parameter keys, etc.). The purpose is to have my code indexed by IDE, have suggestions, auto-corrections / completitions, all the error-proof goodies.
I want to define explicit keys for the constructor of this class EPPZObject
, so I did:
var EPPZ =
{
width: 'width',
height: 'height'
}
var EPPZObject = Class.extend
({
construct: function(parameters)
{
// Constant usage works fine here.
log(parameters[EPPZ.width]);
log(parameters[EPPZ.height]);
}
});
In client code, I can use the constants as well:
log(EPPZ.width); // Logs 'width' just fine.
log(EPPZ.height); // Logs 'height' just fine.
But when I want to use the constats while constructing (that would be the whole point actually), then it just not works:
var objectThatWorks = new EPPZObject(
{
'width' : '9',
'height' : '9',
});
var objectThatNotWorks = new EPPZObject(
{
EPPZ.width : '9',
EPPZ.height : '9',
});
It says:
Uncaught SyntaxError: Unexpected token .
How to get over this? Is there a similarly clean solution to use constants?