how can i use a namespace string with jquery?

240 Views Asked by At

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

1

There are 1 best solutions below

0
On BEST ANSWER

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.