Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within HTML Pages with CSS and JavaScript
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Jeanne Boyarsky
Ron McLeod
Paul Clapham
Liutauras Vilda
Sheriffs:
paul wheaton
Rob Spoor
Devaka Cooray
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Carey Brown
Frits Walraven
Tim Moores
Bartenders:
Mikalai Zaikin
Forum:
HTML Pages with CSS and JavaScript
call object per string
nimo frey
Ranch Hand
Posts: 580
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I have a common js-object:
var TEST = { a: "1", b: "2" }
I know, I can call property a via
TEST.a
or via
TEST['a']
.
I want to do something like this:
['TEST'].a
But it does not work. How can I do that? Should I use eval or call() or something?
Eric Pascarello
author
Posts: 15385
6
posted 12 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
Never use
eval
.
It would be better if you namespaced it and not make it a global variable.
var myObj = {}; myObj.TEST = { a: "1", b: "2" }; console.log( myObj["TEST"].a );
but if you have a thing against using the namespace, you can always use window
var TEST = { a: "1", b: "2" } console.log( window["TEST"].a );
Eric
nimo frey
Ranch Hand
Posts: 580
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Thanks it works, but I how do I solve that, when using module-pattern:
var myModule= (function() { var Submodule= { do: function(){ console.log("do called"); } })();
I want to call Submodule by
string
:
Something like
myModule['Submodule'].do();
does not work.
Eric Pascarello
author
Posts: 15385
6
posted 12 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
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
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Oh, so should I do something like that:
var myModule= (function() { Submodule: { do = function(){ console.log("do called"); } })();
Is this better ?
Eric Pascarello
author
Posts: 15385
6
posted 12 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
That code appears to be the same as it was before.
Your code is the same as:
var foo = (function(){ })(); console.log( foo );
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.
var foo = (function(){ var msg = "hi"; var bar = function() { alert( msg ); }; return bar; })(); console.log( foo() );
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
javascript function name, duplicates, good practices
can this be done?
HTML & Javascript
javascript spinner
Randomly displaying images from a directory with JavaScript?
More...