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