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 key in obj) {
console.log(obj[key]) ;//才能访问 console.log(obj.key); // undefined * 3 : key不再是变量 转化为obj["key"] }