I am doing the codecademy's javascript tutorial and encountering the problem as following:
var friends = {};
friends.bill = {
firstName: "Bill",
lastName: "Gates",
number: "(206) 555-5555",
address: ['One Microsoft Way','Redmond','WA','98052']
};
friends.steve = {
firstName: "Steve",
lastName: "Jobs",
number: "(408) 555-5555",
address: ['1 Infinite Loop','Cupertino','CA','95014']
};
var list = function(obj){
for(var prop in obj){
console.log(prop);
}
};
var search = function(name) {
for(var prop in friends){
console.log(prop);
console.log(friends.prop);//friends[prop] will print out
}
};
list(friends);
search("Steve");
the console.log(friends.prop) will print out undefined but if I changed it to
friends[prop], it will print out the bill and steve object information.
I see the w3c tutorial say that two of the access method is right and I cannot figure out
what is the problem?
Thx in advance.
friends[prop]asfriends.propwill never work.