Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Use variable as object name...

Posted on 2011-04-30
7
Medium Priority
?
237 Views
Last Modified: 2012-05-11
When defining an object inside of another object, I'm trying to do this:

{myObject.config.elementID:'foo'}

but it's keeps erroring out on 'myObject.config.elementID' which is a valid var that's set earlier. Can you not use a variable as a name in an array or is there a way to do it?
0
Comment
Question by:interclubs
  • 3
  • 2
  • 2
7 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 35499157
use the "array" way :

myObject[myObject.config.elementID] = 'foo';
0
 

Author Comment

by:interclubs
ID: 35499193
Still doesn't seem to work, maybe it's something else. This is my code, which appears in a function inside of myObject:

myObject._renderCSSEvents({myObject[myObject.config.elementID] : 'visibility'});

and the error is: Uncaught SyntaxError: Unexpected token [
0
 
LVL 83

Expert Comment

by:leakim971
ID: 35499221

// before creation and init of myObject, myObject.config and myObject.config.elementID

myObject[myObject.config.elementID] = 'visibility';
myObject._renderCSSEvents(myObject.config);

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 20

Expert Comment

by:Proculopsis
ID: 35503225

myObject._renderCSSEvents({myObject[myObject.config.elementID] : 'visibility'});

The problem here is that, unless you prototype type Array or Object, your variable myObject cannot be an array and have methods.
0
 
LVL 20

Expert Comment

by:Proculopsis
ID: 35504026

// Alternatively, if I misunderstood your question:

function asObject( key, value ) {
  var result = {};
  result[key] = value;
  return result;
}

myObjectOne._renderCSSEvents( asObject( myObjectTwo[myObjectOne.config.elementID], 'visibility' ) );
0
 

Accepted Solution

by:
interclubs earned 0 total points
ID: 35713804
After much research, it seems that it's not possible to use a variable as a literal object name.

"he limitation of the literal object syntax is that the names has to be literal. As the names can be specified as an identifer as well as a string, it's not possible to use a variable instead."
0
 

Author Closing Comment

by:interclubs
ID: 35744780
No one else gave the correct answer.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question