Access JavaScript Object Variable Properties
Not all JavaScript objects are as easy as MyObject.property. Sometimes you may want to access a property whose key is stored in a variable. Luckily accessing these properties is very easy.
Javascript Object Property Accessing Example
/* setting */
var myObject = {
left : 30,
top: 20
};
/* basic access */
var left = myObject.left; //OR
var left = myObject['left'];
/* accessing it or variables */
var mode = 'vertical';
var value = myObject[mode == 'horizontal' ? 'left' : 'top'];
You may use array-style syntax to access an object's properties. The string within brackets returns the properties.
I spent a few months experimenting with different approaches for writing simple, elegant and maintainable media queries with Sass. Each solution had something that I really liked, but I couldn't find one that covered everything I needed to do, so I ventured into creating my...
Client-side APIs on mobile and desktop devices are quickly providing the same APIs. Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop. One of those APIs is the getUserMedia API...
Remember the good old days of Windows applications forcing you to scroll down to the bottom of the "terms and conditions" pane, theoretically in an effort ensure that you actually read them? You're saying "No David, don't do it." Too late -- I've done...
Chris Coyier authored a post titled Better Pull Quotes: Don't Repeat Markup a while back. In his post he created great-looking pull quotes without repeating any content -- instead he uses jQuery to dynamically create the pull quotes. The following is the...
I’m a big fan of myObject[key] as its close to the PHP array syntax. Just personal preference really and it gives great flexibility and easy to test for.
@Colin – I just love how object property keys and array indexes are treated as equal in Javascript. The closest to this coolness that PHP comes is in pseudo-property keys ($obj->$var_key)
never knew this method, very useful.
@Chris – PHP’s ArrayObject class allows you to access properties using array syntax.
@keith – huh…never knew that. Thanks! :D
It’s a good thing to point out that you can almost always avoid eval() by use of myObject[‘key’] reference. for example:
A lot of people think you have to do something like:
Avoid eval! Eval is evil.
@Timothy: While it is preferable to avoid eval in code that need not be written in that form, as you show, I would like to contest that eval is far from evil.
It’s a basic language construct upon which the entire functional programming paradigm can be built in languages that do not support it natively. Sometimes a problem is better expressed in said paradigm, but the language prevents you from properly exploiting it’s capabilities. This is one of the many ways eval *can* be used for the better of your programming speed, debug-ability and general style of programming.
So please, next time you wish to share your opinion on eval, try to think of the grand picture this language construct fits in, and don’t spread lies about otherwise finely crafted implementations thereof.
Thank you! I was stumped but this got me back on track.
Thank you soooo much, didn’t know this was possible. Spent countless hours trying to refactor my object to make calls easier, wish someone told me about this earlier lol.