首页 > 代码库 > JavaScript对象属性访问的两种方式

JavaScript对象属性访问的两种方式

JavaScript对象属性访问的两种方式

object.attribute

object["attribute"]

例如:

var employees = [
{ "firstName":"Bill" , "lastName":"Gates" },
{ "firstName":"George" , "lastName":"Bush" },
{ "firstName":"Thomas" , "lastName": "Carter" }
];
alert(employees[0].lastName);
alert(employees[0]["lastName"]);
[javascript] view plain copy
 
  1. var employees = [  
  2. "firstName":"Bill" , "lastName":"Gates" },  
  3. "firstName":"George" , "lastName":"Bush" },  
  4. "firstName":"Thomas" , "lastName": "Carter" }  
  5. ];  
  6. alert(employees[0].lastName); // 方式一  
  7. alert(employees[0]["lastName"]); // 方式二  

JavaScript对象属性访问的两种方式