提示 firstName undefined,实际 friends 里边的 i 是有 firstName 的。
|
1
用 friends[i]。
|
|
2
friends[i]
|
|
4
. 只能访问到关键字,很明显这里的 i 是变量,要用 []
|
|
6
>> for(var i in friends)
>> 第一次循环时,i 的值是字符串 'bill' >> friends.i 等价于 friends['i'] >> 而你想要的是 friends.bill,等价于 friends['bill'] |
|
7
@dorentus friends.bill, friends['bill'] 这个是等价的吧,只是在加载 i 的时候变成了:friends.'bill' ?
|
|
8
噗……楼主还是没习惯。
a.i 就是 a['i'] 那你想,你要得到 friends.bill 那就应该是 friends['bill'] ,想得到 friends.i 就是 friends['i'] 你写的是 friends.i ,等价于 friends['i'] ,它显然和 friends['bill'] 不是同一个东西。 完全不存在friends.'bill'这种东西。 这里也不存在“加载i的时候”怎么样了,因为 friends.i 永远都是 friends['i'] 不可能变成 friends['bill'] |
|
xieziNov 25, 2013
10
这个应该的错误信息是"Cannot read property 'firstName' of undefined",这说明friends.i是没有定义的,而不是你写的“firstName undefined”。@bombless写的很清楚了!
|