1

I would like to create ONE object containing the whole config for certain component. I would liek it too be like this:

var ObjectConfig = {
    fieldKeys : {
        name: "Obj. name",
        state: "Obj. state",
        color: "Obj. color"
    },
    templates : {
        basicTemplate :  [ ObjectConfig.fieldKeys.name, ObjectConfig.fieldKeys.state ],
        altTemplate : [ ObjectConfig.fieldKeys.name, ObjectConfig.fieldKeys.color ]
    }
}

But this in the right way to do it - it doesn't work. How can I achieve my goal?

EDIT: Sorry, I was writing it by hand in a hurry, that's where the syntax errors came from. Now it's correct. The error I get is Uncaught TypeError: Cannot read property 'fieldKeys' of undefined. I guess that doing it this way is impossible - what is the best alternative then?

dstronczak
  • 2,406
  • 4
  • 28
  • 41
  • 1
    Your syntax is incorrect. Use `:` instead of `=` to define object properties – jevakallio Jan 30 '13 at 10:23
  • 1
    + you skipped comma after `"Obj. state"`. – VisioN Jan 30 '13 at 10:24
  • + if you mean referencing to properties in the object itself by `it's own fields`, that can't be done – Cerbrus Jan 30 '13 at 10:26
  • Sorry, I was writing it by hand in a hurry, that's where the syntax errors came from. Now it's correct. Could you please reevaluate my question? – dstronczak Jan 30 '13 at 10:54
  • any error message on the console? – Toping Jan 30 '13 at 10:56
  • I can't find the duplicate, so I'm gonna write an answer :-) – Bergi Jan 30 '13 at 11:05
  • possible duplicate of [Self-references in object literal declarations](http://stackoverflow.com/questions/4616202/self-references-in-object-literal-declarations) – Felix Kling Jan 30 '13 at 11:15
  • @FelixKling: Thanks, that's what I've looked for. Which search terms did you use? – Bergi Jan 30 '13 at 11:21
  • 1
    @Bergi: [`[javascript] object self reference`](http://stackoverflow.com/search?q=%5Bjavascript%5D+object+self+reference)... my answer might not be the best one, but there are a couple of other duplicates as well. – Felix Kling Jan 30 '13 at 11:23

1 Answers1

2

Your problem is that the object is constructed from the literal before it is assigned to the ObjectConfig variable. Therefore, accessing ObjectConfig.fieldKeys inside the literal will lead to the error.

The best solution is to construct first one object only, and then add further properties sequentially:

var ObjectConfig = {
    fieldKeys: {
        name: "Obj. name",
        state: "Obj. state",
        color: "Obj. color"
    }
};
ObjectConfig.templates = {
    basicTemplate:  [ ObjectConfig.fieldKeys.name, ObjectConfig.fieldKeys.state ],
    altTemplate: [ ObjectConfig.fieldKeys.name, ObjectConfig.fieldKeys.color ]
};

Another (shorter) method would an extra variable for the keys object, which is assigned before the construction of the templates object:

var keys, ObjectConfig = {
    fieldKeys: keys = {
        name: "Obj. name",
        state: "Obj. state",
        color: "Obj. color"
    },
    templates: {
        basicTemplate: [ keys.name, keys.state ],
        altTemplate: [ keys.name, keys.color ]
    }
};

To work around the extra variable in global scope, you might use an IEFE. A more readable solution might look like this then:

var ObjectConfig = (function() {
    var keys = {
        name: "Obj. name",
        state: "Obj. state",
        color: "Obj. color"
    };
    return {
        fieldKeys: keys,
        templates: {
            basicTemplate: [ keys.name, keys.state ],
            altTemplate: [ keys.name, keys.color ]
        }
    };
})();
Bergi
  • 630,263
  • 148
  • 957
  • 1,375