可能重复:
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>
- 为什么
foo.cool
返回我不确定哪里为foo[cool] returns me kevin
。 - cool如何在foo对象中实际引用我的名称属性。
因为当对象属性是一个表达式时,使用'[]'表示法,就像变量'cool'(或者包含无法用'.'表示法的字符)。如果使用'.'表示法,则该属性不能是表达式的结果。 –