Possible Duplicate:
Self-references in object literal declarations
var obj = {
value: 10,
value2: value + 2
};
How can I do the above? (Assuming it’s even possible)
I am using a lot of jQuery’s $.extend to add more properties that rely on the previous values being added; so that modifying a few values automatically correct the rest.
You can’t do it in declaration, but you could always make a function. The following will always return a value 2 greater than that stored in value:
If you don’t want the value of value2 to change along with value, you could declare a placeholder variable and declare the object using that: