1

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?

1
  • 1
    You're misusing the Array constructor. Commented Apr 23, 2010 at 0:44

2 Answers 2

2

Your DDROA variable is only assigned after the object is created.
Therefore, when the object is initialized, DDROA is undefined.

To work around it, you should set RouteContext separately, like this:

var DDROA = {
    AllowedRoutes : {
        AR0 : {text : 'SomeText', value : 'SomeValue'},
        AR1 : {text : 'SomeText2', value : 'SomeValue2'}
    }
};
DDROA.RouteContext = {
    RC0 : {text : 'None', value : '0',
        AllowedRoutes : [ DDROA.AllowedRoutes.AR0 ]  //An error does not occur here
    }        
};

Also, when given a single argument, the Array constructor takes the length of the array.

To make an array with a single element, use an array literal, like this [ DDROA.AllowedRoutes.AR0 ].


To answer your edited question, the code inside the function is only executed when the function is called, which is after the variable is assigned.

Sign up to request clarification or add additional context in comments.

8 Comments

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().
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, 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: I mis-nested the braces. It works now. (I tried it) Mike is correct; your Array constructor use is valid.
Slacks could you reveiw my edited answer I just dont understand.
|
0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.