2

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

2
  • if you want to write even less, you could have it $.pg = { w: 700... and then just do $.pg.w Commented Aug 16, 2012 at 21:35
  • 2
    Note that this isn't really a jQuery issue, it's a JavaScript syntax issue. Commented Aug 16, 2012 at 21:38

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.