Possible Duplicate:
JavaScript property access: dot notation vs. brackets?
<script>
var foo = {
name: 'kevin'
};
document.write(foo.name);
document.write(foo['name']);
var cool = 'name';
document.write(foo.cool);
document.write(foo[cool]);
</script>
- Why does
foo.coolreturns me undefined where asfoo[cool] returns.
me kevin - How does cool actually refer to my name property in foo object.
A
coolproperty is not defined onfoo, sofoo.coolis undefined. If you didfoo.name, it would returnkevin.coolinfoo[cool]is the one you defined in the line above it, so it is actuallyfoo['name'], which is defined and has the valuekevin.