My JavaScript code includes a "singleton" class like this:
ExpressionValidator =
{
// A map of function names to the enumerated values that can be passed to it.
functionParamValues:
{
"Func1": ["value1", "value2", "value3"],
"Func2": ["value1", "value2", "value3"]
}
}
I'm looking for a way to define the array ["value1", "value2", "value3"]
in a more efficient manner, rather than duplicating it. However, I am struggling to make it work.
One approach I have tried is:
ExpressionValidator =
{
// An array of possible values to use as parameters to certain functions.
values:
[
"value1",
"value2",
"value3"
],
// A map of function names to the enumerated values that can be passed to it.
functionParamValues:
{
"Func1": values,
"Func2": values
}
}
Unfortunately, this solution doesn't work because it cannot find the values
array when initializing the "Func1" and "Func2" properties within the functionParamValues
object. I also attempted using this.values
instead of just values
, but it returns null
, indicating that it's trying to access the values
property of the functionParamValues
object rather than its parent ExpressionValidator
.
I understand that I could resolve this issue by moving the values
array outside of the ExpressionValidator
definition, but I prefer to keep it contained within if feasible to avoid conflicts with the global namespace. This should be considered only as a last resort if no other alternatives are available.