0

I have simple object as follows:

var obj={
 address:"http://localhost:8080",
 ajax:{
  login:address+"/login"
 }
}

I am getting error here login:address+"/login"

Can't I access address variable ?

display-name-is-missing
  • 4,424
  • 5
  • 28
  • 41
Rajeev
  • 312
  • 3
  • 10
  • possible duplicate of [Self-references in object literal declarations](http://stackoverflow.com/questions/4616202/self-references-in-object-literal-declarations) or [Access object properties within object](http://stackoverflow.com/questions/12789141/access-object-properties-within-object) or many many others – user229044 Feb 07 '14 at 19:50

1 Answers1

1

You can't access a value in an object that isn't defined yet. i.e. obj.address doesn't exist because you're not done defining it yet.

You'd need to define the variable outside the object first.

helion3
  • 34,737
  • 15
  • 57
  • 100
  • OP uses `address` not `obj.address`. – Ram Feb 07 '14 at 19:49
  • He does, but `address` doesn't refer to anything in the object `obj`. The link posted for how to do self-referential values provides a solution, I was just answering why he can't access it. – helion3 Feb 07 '14 at 19:54