Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

call object per string

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a common js-object:



I know, I can call property a via or via .

I want to do something like this:



But it does not work. How can I do that? Should I use eval or call() or something?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never use eval.

It would be better if you namespaced it and not make it a global variable.


but if you have a thing against using the namespace, you can always use window



Eric

 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks it works, but I how do I solve that, when using module-pattern:



I want to call Submodule by string:

Something like

does not work.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Becuase you are storing a result of a function to a variable and the function is not returning anything to store in that variable.

Eric
 
nimo frey
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, so should I do something like that:




Is this better ?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code appears to be the same as it was before.

Your code is the same as:



All of the stuff inside is local variables, there are no way you can access them.

If you want something to be accessed outside of it, you would have to return it.


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic