5

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.

Community
  • 1
  • 1
Nahydrin
  • 13,197
  • 12
  • 59
  • 101
  • possible duplicate of [Self-references in object literal declarations](http://stackoverflow.com/questions/4616202/self-references-in-object-literal-declarations) and [Object referencing its own property on initilization](http://stackoverflow.com/questions/7433395/object-referencing-its-own-property-on-initilization). – Felix Kling Mar 14 '12 at 19:13

2 Answers2

4

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:

var obj = {
    value: 10,
    getValue2: function(){ return this.value + 2; }
};

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:

var placeholder = 10;

var obj = {
    value: placeholder,
    value2: placeholder + 2
};
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • building on this answer, you could make value and value2 private variables and provide obj with a generic "get" function. – jbabey Mar 14 '12 at 19:17
  • That should be `this.value` else you get error: `'value' is undefined`. – gilly3 Mar 14 '12 at 19:25
  • @gilly3 - Meant to have it there...typo. Fixed. Thanks for pointing it out. – Justin Niessner Mar 14 '12 at 19:26
  • One point that should be made is that the two aren't functionally equivelant. In the first example, after initialization, `obj.value2` is unrelated to `obj.value`. But, in your solution, when you increment `obj.value`, the result of `obj.getValue2()` is also incremented. http://jsfiddle.net/8zWNs/ – gilly3 Mar 14 '12 at 19:49
  • @gilly3 - That's true, but if the OP truly wanted the behavior that you're talking about it would be easy enough to define the object without referencing a previous value. – Justin Niessner Mar 14 '12 at 19:52
  • Right, absolutely. If the values should be tied together, a function is absolutely the way to go. – gilly3 Mar 14 '12 at 19:55
1

I am not 100% sure if you can do this at the time of declaration. You could, however, do it afterwards:

using extend:

var obj = {
    value: 10
};

$.extend(obj, {
    value2: obj.value + 2
});

http://jsfiddle.net/KgKgf/

just javascript:

var obj = {
    value: 10
};

obj.value2 = obj.value + 2;

http://jsfiddle.net/KgKgf/1/

gilly3
  • 87,962
  • 25
  • 144
  • 176
jbabey
  • 45,965
  • 12
  • 71
  • 94
  • Holy smokes, why would anyone use `.extend()` just to add a property to an object? Way overkill! Your second example is correct, but that first one is ... silly. – gilly3 Mar 14 '12 at 19:27
  • @gilly3 i agree that it is not the proper tool for this job, but the OP mentioned he was using extend to add properties so i figured i would show an example using extend. – jbabey Mar 14 '12 at 19:33
  • Fair enough... I missed that part in his question. – gilly3 Mar 14 '12 at 19:38
  • @gilly3 thank you for removing your downvote :D – jbabey Mar 14 '12 at 19:39