我仍然在學習JavaScript模式,我對此很好奇。在JS對象數組中,我應該使用鍵來標識每個對象嗎?
如果我有一個對象數組,我應該讓對象數組沒有鍵(例A),還是使用它們的鍵(例B)?
對象的數組沒有任何按鍵:
var soylent_green_candidates = [{
id: 12341,
name: "Don",
gender: "Male",
weapon_of_choice: "Kangaroo Cannon"
},
{
id: 24325,
name: "Jimmy",
gender: "Male",
weapon_of_choice: "Sushi Blaster"
},
...
]
或者使用唯一的ID作爲密鑰的對象數組:
var soylent_green_candidates = [
12341: {
name: "Don",
gender: "Male",
weapon_of_choice: "Kangaroo Cannon"
},
24325: {
name: "Jimmy",
gender: "Male",
weapon_of_choice: "Sushi Blaster"
},
...
]
示例2的語法不正確。 – m90
您不能使用以數字開頭的''identifier'',如例2 – sarbbottam
Lol @ soylent green所使用的。 –