Contact list: List 'em all!


#1


List 'em all!


My code works, but I can't understand how the last five lines actually work. If someone can explain it to me, I would be grateful.


var friends = {
     bill: {
         firstName: "Bill",
         lastName: "Gates",
         number: 0123456789,
         address: ["It doesn't matter, he can buy them all", "still"]
         },
     steve: {
         firstName: "Steve",
         lastName: "Jobs",
         number: 0987456321,
         address: ["Gravyard", "Dark", "Humor"]
         }
    };
    var list = function (obj) {
        for (var anything in obj) {
            console.log (anything);
        }
    }


#2

lets add a function call:

var list = function (obj) {
        for (var anything in obj) {
            console.log (anything);
        }
    }
list(friends);

good, when you call the function you pass friends (the object) as argument, so obj (the function parameter) now holds your object. then the for in loop loops over the property's (bill and steve) which are stored in anything (bill and steve are objects, they are not first names Bill and Steve, don't confuse the two)

so the for in loop is doing all the work, its (in turn) assigns the property's in your friends object to anything, which you can then log to the console


#3

Thank you for the tutorial. Now all that is left is little practice.


#4