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.
Firefox OS is all over the tech news and for good reason: Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript. Firefox OS has been rapidly improving...
MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does. Many developers create their classes as globals which is generally frowned up. I mostly disagree with that stance, but each to their own. In any event...
Jonathan Snook debuted a great tutorial last September detailing how you can use an image and a few jQuery techniques to create a slick mouseover effect. I revisited his article and ported its two most impressive effects to MooTools.
The Images
These are the same...
The lightbox is probably one of my favorite parts of the Web 2.0 revolution. No more having to open new windows (which can bog down your computer quite a bit) to see a larger image, video, etc. Instead, the item loads right into 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.