I’m creating an object using literal notation. Is it possible to have some of the properties use previously defined properties as their value?
For example:
var test = {
prop1: obj1,
prop2: obj2,
prop3: (prop1!=null)?prop1:prop2
};
If you are trying to do something like
var x = { 'a': 1, 'b': x.a }then it won’t work. Sincexis not finished being defined.But you can do something like
This is because each var definition is interpreted sequentially. Basically equivalent to
But with objects and arrays the entire definition is interpreted one time.