Reputation: 8359
I have the following code example...
$.pg = {
width : 700,
height: 200,
rate: 30
};
Is there a convenient way I can write $.pg[width], rather than $.pg['width'] all the time to get 700? The whole point of me putting width, height and rate into pg, is so I can write less.
Thanks
Upvotes: 2
Views: 233
Reputation: 707876
You can use:
$.pg.width
$.pg.height
$.pg.rate
to refer to those properties. If the property name is known at code writing time (like it is here), then you can use either the dot syntax .propName
or ["propName"]
but as you've noticed, the dot syntax is shorter. If the property name is not known at code writing time (and thus is stored in a variable), then you have to use the [variableName]
syntax.
Upvotes: 6