1

If I wanted to access a value within itself (an object), how would I go about doing so?

Here's what I have (simplified):

options = {
    'image' : {
        'height' : {
        'upper' : 100,
        'lower' : 25
        },
        'width'  : {
        'upper' : 100,
        'lower' : 25
        },
    'dimensions' : (Math.floor(Math.random() * (options.image.height.upper - options.image.height.lower) + options.image.height.lower)) + 'x' +                                                         (Math.floor(Math.random() * (options.image.width.upper - options.image.width.lower) + options.image.width.lower())),
        'color' : 'fff'
};

If you look at options.dimensions, I'm attempting to access the values in height and width, yet I believe my scope is wrong.

Is there a way to access what I need the way I'm doing it? If not, what's a better way to go about this?

Thanks :)

Will
  • 914
  • 3
  • 11
  • 19

2 Answers2

4

Do it in two steps:

options = {
    'image' : {
        'height' : {
            'upper' : 100,
            'lower' : 25
        },
        'width'  : {
            'upper' : 100,
            'lower' : 25
        },
        'color' : 'fff'
    }
};

options.dimensions = (Math.floor(Math.random() * 
    (options.image.height.upper - options.image.height.lower) + options.image.height.lower)) + 
    'x' + (Math.floor(Math.random() * (options.image.width.upper - options.image.width.lower) +
    options.image.width.lower()));
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471
1

Why don't you try

options = {
    'image' : {
        'height' : {
        'upper' : 100,
        'lower' : 25
        },
        'width'  : {
        'upper' : 100,
        'lower' : 25
        },
        'color' : 'fff'
};

options['dimensions'] = (Math.floor(Math.random() * (options.image.height.upper - options.image.height.lower) + options.image.height.lower)) + 'x' + (Math.floor(Math.random() * (options.image.width.upper - options.image.width.lower) + options.image.width.lower()));

I don't think there any way to refer an object which still not created.

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531