Quick question on for/in loops


#1

I made it through this exercise but not really sure why.

on my function, how does it know that obj is an actual obj of "friends "and prop is a property of that object
if they aren't really defined?

can someone explain this to me?

var friends = {};
friends.bill = {
    firstName: "Bill",
    lastName: "Boden",
    number: 7777474747,
    address: ['A street apart', 'Orlando', 'FL','33333']
    };
friends.steve = {
    firstName: "Steve",
    lastName: "Chon",
    number: 5555555555,
    address: ['Another street', 'Miami', 'FL', '44444']
    };
    
    var list = function(obj){
        for(var prop in obj){
            console.log(prop);
            }
        }

#2

It doesn't know it yet, but it will when you call the function. When you call it you will feed it an object, like this:

list(friends);

Your function is expecting that, list = function(obj) and will assign friends value to obj.

This is just the way for..in loops work.

I don't know if you've noticed yet, but you don't have to use obj and prop. You can use anything, almost.

for(var item in items)
for(var dog in pack)
for(var student in myClass)

Use variable names that make your program easy to understand.


#3

got it! thanks alot!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.