wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes call object per string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "call object per string" Watch "call object per string" New topic
Author

call object per string

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jun 28, 2008
Posts: 580
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jun 28, 2008
Posts: 580
Oh, so should I do something like that:




Is this better ?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
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.


 
 
subject: call object per string