Is there any way to make something like the following work in JavaScript?
var foo = { a: 5, b: 6, c: this.a this.b // Doesn't work };
In its current form, this code obviously throws a reference error because this
does not reference foo
. ButyesIs there any way to make the value in an object literal property dependent on other previously declared properties?
You can do this:
This will be some kind of one-time initialization of the object.
Note that you are actually assigning the return value of
init()
tofoo
, so you mustreturn that value
.Well, the only thing I can tell you is