Object Notation

R

RJ

I am having a terrible time getting the following code to work:

var foo = {
bar : "bar",
fooTwo : {
bar : foo.bar
}
}

When this code runs, I am told that foo.bar does not exist in line 4. Why am
I getting this error?
 
R

RobG

RJ said:
I am having a terrible time getting the following code to work:

var foo = {
bar : "bar",
fooTwo : {
bar : foo.bar
}
}

When this code runs, I am told that foo.bar does not exist in line 4. Why am
I getting this error?

Most probably because the JS interpreter hasn't finished creating foo
yet, so it can't access its properties.

var foo = { bar : "bar"};
foo.fooTwo = { bar : foo.bar };

Works and is shorter.
 
R

RJ

That makes sense. Thank you :)

RobG said:
Most probably because the JS interpreter hasn't finished creating foo yet,
so it can't access its properties.

var foo = { bar : "bar"};
foo.fooTwo = { bar : foo.bar };

Works and is shorter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top