DAY 16
1
Modern Web

## 物件的寫法

### 物件實字（Object Literals）

``````var 物件名稱 = {
屬性A: 值A,
屬性B: 值B
};
``````

``````var class = {
tracherName: "John",
students: 30,
firstFloor: true,
function greeting(){
console.log("Hello!!!");
},
subjectTeacher: {
math: 'Tim',
chinese: 'Meg'
}
};
``````

### 建構式

``````var 變數名 = new Object('物件名');
``````

``````var example = {
\$: false,
64: true,
"加密檔案": true,
紙質文件: true
};

example.64; //出現錯誤
example."加密檔案"; //出現錯誤
example.\$; //可以運行
example["64"]; //可以運行
example.加密檔案; //可以運行
example["加密檔案"]; //可以運行
example.紙質文件; //可以運行
example["紙質文件"]; //可以運行
``````

## 物件的運用

``````var family = {};
``````

### 新增資料

``````family.father = "Bruce";
family.kidsName = ["John", "Jason", "Tim"];
family.haveCar = false;
``````
1. 用半形中括弧（`[]`）包覆要命名的屬性，屬性要接半行引號（`''`
``````family['father'] = "Bruce";
family['kidsName'] = ["John", "Jason", "Tim"];
family['haveCar'] = false;
``````

### 讀取資料

``````family.father;
family.kidsName[0];
``````
``````family['father'];
family['kidsName'][0];
``````

ex：

``````var paper = '紙質文件';
var example = {
\$: false,
64: true,
紙質文件: true
};

example.\$; //可以運行
example['\$']; //可以運行
example.64; //出現錯誤
example["64"]; //可以運行
example[paper]; //可以運行
``````

JS讀書筆記30