Well, the question may be not good formulated, but I'll try to get it more clear by example:
var test = {
x1 : 10,
x2 : 20,
x3 : x1 + 5,
x4 : x2 + 5,
}
What I need, is refer to "x1" or "x2" field, which was previously declared. But, if I wrote like in example, JS says, that "x1 is not defined".
My second try was:
var test = {
x1 : 10,
x2 : 20,
x3 : this.x1 + 5,
x4 : this.x2 + 5,
}
But "x3" and "x4" become "NaN", because "this.x1", and "this.x2" is "undefined".
I even tried:
var test = {
x1 : 10,
x2 : 20,
x3 : test.x1,
x4 : test.x2,
}
But get "test is undefined".
So, how can i refer, to "x1" or "x2"?
P.S. I know, that I can write something like:
var test2 = new (function(){
this.x1 = 10;
this.x2 = 20;
this.x3 = this.x1 + 5;
this.x4 = this.x2 + 5;
})();
And it will work just fine, but I really need a JSON-like object definition.
Update: I forgot that this description of object is called a "literal". Because of that I couldn't find existing questions about this theme. However, now I've read similar questions and answers for them, and have not found answer, that solve my problem.
In fact I need to remain in literals notation, because I am developing a "configuration file format", that must have a declarative syntax (and parser for this format of course, that will execute some algorithm according to this configuration file). And this configuration file must be understandable and editable by people, who don't understand JS. For them it's not a script, or object, but just a JSON with specified structure. So I can't use some init function in the end.
Even getters will add too many complexity. It may sound funny, but yes, it's too complex. It will add keywords "get", and "return", and also round brackets and semicolon punctuation. When JSON syntax have only two type of brackets and two type of punctuation: colon and comma. So the complexity almost doubled with getters or functions, even if they would be consists of only one string.
For now I'm leaning towards option, just to add the "id" field, and referenced by it. But maybe someone could offer a better solution.
P.S. I dont have any option other than use some valid JS for this configuration file, because my application would not have any server side. Only local JS and HTML file, and only standard browser for run them, so no file access. Because of that configuration file will be provided to application in form of JS file.