---
# System prepended metadata

title: JS object(一)

---

# JS object(一)

物件 ＝ 屬性＋行為
object = key + value

```javascript
let hero = {
    power:  100 ,
    age: 20 ,
}
```
在這裡 
key = power,age
value = 100,20

如果想印出裡面的屬性，有兩種寫法
```javascript
console.log(hero["age"]);
console.log(hero.age);
```

如果想更改key的value
```javascript
hero.age = 18
```
更改的時候要小心，如果裡面沒有此物件會加進去
舉個例子age打錯打成agE
```javascript
let hero = {
    power:  100 ,
    age: 20 ,
}
hero.agE = 18
console.log(hero)
```
會印出{ power: 100, age: 20, agE: 18 }

#

物件裡面也可以加入function
```javascript
let hero = {
    power:  100 ,
    age: 20 ,
    attack: function(){
        console.log("attack!!!");
    }
}
```
