views:

165

answers:

2

Hello all,
It seems to me that this should work but I cant see what exactly is the problem.

The error Im receiving is "DDROA is not defined"

Could anyone help enlighten me.

var DDROA = {
    AllowedRoutes : {
        AR0 : {text : 'SomeText', value : 'SomeValue'},
        AR1 : {text : 'SomeText2', value : 'SomeValue2'}
    },
    RouteContext : {
        RC0 : {text : 'None', value : '0',
            AllowedRoutes : new Array(
                DDROA.AllowedRoutes.AR0  // An error occurs here
            )
        }
     }
}

EDIT

For Slack's Comment Can you help explain why I must finish declaring the DDROA.AllowedRoutes and then make another statement to add DDROA.RouteContext in a separate stament. Essentially you are telling me I must

var DDROA = {AllowedRoutes : {}};

then

DDROA.RouteContext = {};

Why the two separate statements. I do things like

var Utilities = {
  TextBased : {
    someFunction : function(){ 
      //do stuff 
    },
    someFunction2 : function() {
      Utilities.TextBased.someFunction();
    }
  }
};

What is the difference? It seems to me I should get the same error?

+2  A: 
SLaks
Tried this and still get the same error..same line. Maybe some explination of why you think using a literal array definition would work instead of new Array().
John Hartsock
The array constructor will correctly create an array with a single element if passed a single non-integer argument. That said, probably better practice to avoid it ;o)
Mike Pelley
Mike, I believe I have proven that the arry constructor will not create an array with a single element when passed a single non-integer argument. Either you miss worded your comment or I am not understanding your comment.
John Hartsock
@John: I mis-nested the braces. It works now. (I tried it) Mike is correct; your `Array` constructor use is valid.
SLaks
Slacks could you reveiw my edited answer I just dont understand.
John Hartsock
@John: The array constructor will create an array with a single element when passed a non-integer argument. Your issue was different, where the value you were attempting to assign was not yet defined.
Mike Pelley
so in this case If I trying to predefine an array I should do it outside of where I am going to use it. Im basically using these associative arrays to kinda namespace my javascript. If I predefine arrays I should probably do this in another namespace. Am I correct? Hope that makes sence..
John Hartsock
Another vote not to use the Array constructor. The literal is more readable and less ambiguous!
Juan Mendes
A: 

The reason your second example works is because you're defining a function, and so the code within it isn't executed until you invoke the function, at which point your object is fully instantiated and the reference to the child object is valid. In your first example the object doesn't yet exist when you attempt to access it.

Blair Mitchelmore