Naming conventions: List 'em all! why DOES this function work?


My code behaves fine, I just can't understand why. I created my object and then had a massively difficult time trying to write the function - why doesn't it matter what i name the key or the object? How does this function know that i want to console.log Bill and Steve when there's no indication or callback to that particular object?

Thanks to anyone who can clarify this for me!

var friends = {
        Bill: {
            firstName: "Bill", 
            lastName: "Nylander", 
            number: "617-339-0000",
            address:['1 Maple Leaf Square', 'Toronto', 'Ontario']
        Steve: {
            firstName: "Steve", 
            lastName: "Stamkos", 
            number: "617-339-0001",
            address: ['401 channelside drive', 'Tampa', 'FL', '12113']
var list = function(dudes) {
  for (var pals in dudes){


Well, to clarify its nice to add a function call:

var list = function(dudes) {
  for (var pals in dudes){

now the argument (friends) is copied into the function parameter (dudes) which is why it doesn't matter what you name the function parameter

the for in loop takes care all of the work for you. Its actually really simply. You declare a iterator (pals in your case) which gets assigned all properties of the object in order.

JavaScript does all this for you


Ah got it I think - did it work without the function call just because there is only one object and one function in my exercise?

In a case where I have a .js file with a number of objects, that function call would be more important, I'm assuming?


Function don't execute unless called. So your function was correct, it just didn't execute. This seemed to have slightly confused you because you can name the parameter anything you like

for the rest, i don't understand your question to be honest

You can also leave out the parameter and argument, but then you have to loop over friends (your object), this is also possible


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