嗨嗨!我是Winnie溫尼~
前面兩個章節,我們學到了用[]包覆住的是陣列,用來將相同性質的資料集合起來,而今天要介紹的是用{}包覆住的物件,物件是用來記錄詳細的細部資訊~!
物件的值要使用{}包覆!
let 變數名稱 = {
屬性:值,
⋮
}
例:
let me = {
name:"Winnie",
age:18,
ateDinner:true,
}
變數名稱.屬性
let me = {
name:"Winnie",
age:18,
ateDinner:true,
}
console.log(me.name) // "Winnie"
變數名稱.["屬性"]
let me = {
name:"Winnie",
gender:"female",
age:18,
ateDinner:true,
}
console.log(me.["name"]) // "Winnie"
變數名稱.屬性 = 值
例:
let me = {
name:"Winnie",
age:18,
ateDinner:true,
}
//增加:我的性別是女生
me.gender="female"
//修改:修改我吃過晚餐的狀態
me.ateDinner=false
console.log(me)
//let me = {
// name:"Winnie",
// age:18,
// gender:female,
// ateDinner:false,
//}
delete 變數名稱.屬性
例:刪除我吃過晚餐的狀態
let me= {
name:"Winnie",
age:18,
ateDinner:true,
}
delete me.ateDinner;
console.log(me)
//let me = {
// name:"Winnie",
// age:18,
// gender:female,
//}
新手上路,如文章有錯誤或需修正之處,懇請大家多多指教!
那麼,我們明天見囉~