obj.name ==> obj["name"] 底层的自动转化,所以直接写 obj["name"] 效率会高一些
var obj = { name: "zhangsan", age: 12, sex: "male" } console.log(obj.name); // "zhangsan" console.log(obj["name"]); // "zhangsan"
console.log(obj[name]); // 会报错: name is not defined
常见错误: 变量字符串化
var obj = { name: "zhangsan", age: 12, sex: "male" } for (var prop in obj) { console.log(obj.prop); // undefined * 3 : prop不再是变量 转化为obj["prop"] }