1

## 從 0 學習 JavaScript 筆記－Object & Array 物件與陣列的綜合應用

``````var a =1;
``````

``````function name(params) {

};
``````

``````var array = [0, 1, 2],
``````

``````var object={
text:"string",
number: 10,
}
``````

## 物件 + 函式

``````  var home = {
mom: "Min",
son: ["Joshua", "John"],
chair: 4,
table: 3,
}
``````

``````var home = {
mom: "Min",
son: ["Joshua", "John"],
chair: 4,
table: 3,
dinner: function () { //建立函式
}
}

home.dinner(); //執行物件 home 裡面的函式 dinner 的內容
``````

### 存取物件中函式資料

``````var home = {
mom: "Min",
son: ["Joshua", "John"],
chair: 4,
table: 3,
dinner: function () { //建立函式
}, //新增新物件要用逗點隔開
furniture: function () {
var furnitureNum = home.chair + home.table; //增加一個區域變數，把物件中的要抓取的屬性放進來
console.log(furnitureNum); //執行函式中的區域變數內容

}
}

home.dinner(); //執行物件 home 裡面的函式 dinner 的內容 ()
home.furnitureNum(); //執行物件 home 裡面的函式中的區域變數，結果會得到 7
``````

## 陣列 + 物件

``````var homes = [{
mom: "Min",
son: ["Joshua", "John"],
chair: 4,
table: 3,
},{
mom: "Nancy",
son: ["Ethan", "Elijah", "Emma"],
chair: 5,
table: 4,
}];
``````

### 存取陣列中的物件資料

``````var homes = [{
mom: "Min",
son: ["Joshua", "John"],
chair: 4,
table: 3,
},{
mom: "Nancy",
son: ["Ethan", "Elijah", "Emma"],
chair: 5,
table: 4,
}];