1
<script type="text/javascript">
var sumobj = { 'foo':"apple",'bar':"banana" };
var prop1 = 'foo';

document.write( sumobj.prop1 + "<br />" );
</script>

I'd like to be able to do something similar to the above but this displays 'undefined' presently.

Is there anyway to do this, or something close to it?

Mr Griever
  • 4,014
  • 3
  • 23
  • 41
  • 1
    On the off-chance I tried using [] syntax while screwing around. Makes sense...in a javascript way. In case anyone has a similar question use obj_instance[variable] – Mr Griever Nov 04 '11 at 22:51
  • In javascript, objects and associative arrays are the same thing, I believe. – Christian Mann Nov 04 '11 at 22:52
  • @pimvdb good catch. thanks for linking over there. – Mr Griever Nov 04 '11 at 22:52
  • @MattH. you should accept [wutz's](http://stackoverflow.com/questions/8016594/dynamically-reference-class-properties/8016616#8016616) answer below – puk Nov 04 '11 at 23:22

1 Answers1

7

Do you mean this?

sumobj[prop1]
wutz
  • 3,204
  • 17
  • 13