Help with 6 - why does this work?


#1

This code works, but I don't understand why.

var friends = {};

friends.steve= {
    firstName: "Steve",
    lastName: "jobs",
    number: 9876543,
    address: ['1', 'a', 2]
};

friends.bill = {
    firstName: "Bill",
    lastName: "gates",
    number: 4561234,
    address: ['5', 'b', 6]
};

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

list();

it outputs "steve" and "bill" on subsequent lines.

however, how does list(); know to use the friends object? var list = function(obj) is intelligently grabbing the friends object? I don't see how they are linked.


#2

The Above Code in a normal compiler will result in an error, since you didnt provide anything inside the paranthesis.. It wouldnt even execute the list function, since it doesnt match it(Polymorphism)... This should not output steve and bill.

Proof? Here is proof : https://repl.it/CRD2/0


#3

Awesome, that's super useful! Thanks for the resource, and thanks for the quick reply, too. :slight_smile:

Odd that it seems to work within the context of this lesson. I felt something was amiss.


#4

Codecademy has some extra stuff running "behind the scenes" if you will. Even if you don't specify the list with the correct parameter, it forces itself inside your code without you seeing.