0
我是新來的Javascript,目前學習它codecademy.com訪問屬性
這裏的一部分,我不明白:
var friends = {};
friends.bill={};
friends.steve={};
friends.bill={
firstName:"Bill",
lastName:"Will",
number:4164567889,
address: ['298 Timberbank blvd', 'scarborough', 'ontario']
};
friends.steve={
firstName:"Steve",
lastName:"Evan",
number:4161233333,
address: ['111 Timberbank blvd', 'scarborough', 'ontario']
for (var keys in friends){
console.log(keys); // 1. returns 2 objects of friends(bill, steve)
console.log(keys.firstName); // 2. why is this wrong?
console.log(friends[keys].firstName); // 3. why the properties has to be accessed in this way?
}
迄今爲止我唯一能假設是「for..in」循環返回「朋友」中的對象數組。這就是爲什麼它必須使用[]符號來訪問。糾正我,如果我錯了。謝謝
您可以閱讀MDN文檔中的任何JS語法構造:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in。 –
@FelixKling,thx的信息 – shensw