How can a Javascript object refer to values in itself?

Posted by Erin Drummond on Stack Overflow See other posts from Stack Overflow or by Erin Drummond
Published on 2010-05-07T08:39:14Z Indexed on 2010/05/07 8:58 UTC
Read the original article Hit count: 293

Filed under:
|
|

Hi,

Lets say I have the following javascript:

var obj = {
 key1 : "it ",
 key2 : key1 + " works!"
};
alert(obj.key2);

This errors with "key1 is not defined". I have tried

this.key1
this[key1]
obj.key1
obj[key1]
this["key1"]
obj["key1"]

and they never seem to be defined.

How can I get key2 to refer to key1's value?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about JSON