0

I have a "singleton" class in Javascript declared like the following:

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 would like to somehow define the array of ["value1", "value2", "value3"] in such a way that it doesn't have to be copy-pastaed everywhere that it is needed, but I cannot figure out how to make it work.

My current idea looks something like this:

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
    }
}

But this doesn't work because it can't find the values array when trying to initialize the "Func1" and "Func2" properties of the functionParamValues object. I also tried using this.values instead of just values but it ends up as null so I assume it's trying to get the values property of the functionParamValues object, not the parent ExpressionValidator.

I know this can be solved by moving the values array outside of the ExpressionValidator definition, but I would like to keep it inside if possible so that it doesn't conflict with anything in the global namespace. This should only be a last resort if there's really no other way to do it.

Justin G
  • 776
  • 1
  • 10
  • 25
  • To me, a "class" has some methods. You just have a plain data object. – Bergi Apr 05 '14 at 20:20
  • This is just a bare-bones example. There are plenty of functions and other things I left out of the example since it doesn't matter for the sake of this problem. This does seem to be a duplicate issue now that I've looked at that other one you linked. Is there some way to mark the question as a duplicate? I don't know what to do in this situation. – Justin G Apr 06 '14 at 18:03
  • That happens when 5 people [vote to close as a duplicate](http://stackoverflow.com/help/privileges/close-questions). You, with 141 reputation, might [flag it](http://stackoverflow.com/help/privileges/flag-posts) – Bergi Apr 06 '14 at 20:37

2 Answers2

1

When writing a literal like that without any function scope to set the value this, you'll have to wrap everything in IIFE's to get the values.
A better pattern to do what you want would be

var ExpressionValidator = {};

ExpressionValidator.values = [
    "value1",
    "value2",
    "value3"
];

ExpressionValidator.functionParamValues = {
    Func1: ExpressionValidator.values,
    Func2: ExpressionValidator.values
}

FIDDLE

If you had actual functions, it would be a different matter

var ExpressionValidator = {
    values: [
        "value1",
        "value2",
        "value3"
    ],
    Func1: function() {
        console.log( this.values );
    },
    Func2: function() {
        console.log( this.values );
    }
}
adeneo
  • 312,895
  • 29
  • 395
  • 388
1

Define the array inside a closure if you don't want it in the global scope.

var ExpressionValidator = (function() {
    var values = [ 
        'value1',
        'value2',
        'value3'
    ];       

    return {
        values: values,
        functionParamValues: {
            Func1: values,
            Func2: values
        }
    };
})();
Russ Ferri
  • 6,459
  • 2
  • 22
  • 24