JavaScript Object Properties 的存取

 

例如有一 JavaScript object 如下

var person = {fname:"John", lname:"Doe", age:25}; 

則該 object 底下的屬性(property)將會有 fname、lname、age 這三個。

 

要存取一個屬性有三種方式,承上例

1、person.age;

2、person["age"];

3、var x = "age"; person[x];

 

新增屬性

var person = { fname: "John", lname: "Doe", age: 25 };
person.nationality = "English";
console.log(person.nationality);

 

刪除屬性

var person = { fname: "John", lname: "Doe", age: 25 };
console.log(person.age);
delete person.age;
console.log(person.age);

 

參考資料:

Accessing Object Properties