Possible Duplicate:
Self-references in object literals / initializers
Can this be done? (obviously not in this syntax)
var a = {
    b : 10,
    c : this.b * 2 // returns 'undefined'
};
I have also tried
var a = {
    b : 10,
    c : a.b * 2 // throws error 'a is undefined'
};
and
var a = {
    b : 10,
    c : b * 2 // throws error 'b is undefined'
};
It makes sense to me that these values are undefined, I have not finished defining them. However it seems to me like there would be a solution to structuring a object like that and having c be conditional on b
Alternatively, you can use a self starting function to give you a similar affect to what you are looking for:
var a = (function() {
    var b = 10;
    return {
        b:b,
        c:b*2
    }
})();
console.log(a.c);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With