4

I've been having some trouble making sense of two javascript functions in this contact list project on codecademy.

Specifically, I got confused with conditions like, "obj" or "prop". I would really appreciate it if someone can explain in detail how these functions work.

Here is the code & thank you:

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) {
    if(friends[prop].firstName === name) {
      console.log(friends[prop]);
      return friends[prop];
    }
  }
};

list(friends);
search("Steve");
0

2 Answers 2

0

obj is just the name of the parameter of the list function that you are creating. It has no special meaning. You could call it foo or object, or anything else that makes sense to you. The value of the argument you pass to the function call of list (above, namely friends) is stored in the parameter in the scope of the function. That is to say, obj essentially becomes friends while operating inside the code of list.

prop is similar: it's just a variable that is created as part of JavaScript's for...in syntax. for in loops over the property names of the object that is the argument to the in construct and stores them one by one in prop. Again, you could call this whatever you wanted:

var list = function (foo) {
    for (var bar in foo) {

However, as I'm sure you've learned, it makes sense to give variable names some meaning, so obj is short for "object" as the list function operates on any general object, and prop is short for "property."

Keep in mind that for...in loops over property names. To access the correspoding value, you should use:

if (obj.hasOwnProperty(prop)) {
    //access via obj[prop];
}

The search function actually does this, but without the recommended hasOwnProperty check.

Sign up to request clarification or add additional context in comments.

2 Comments

You're explanation of the for (var prop in obj) { syntax is really helpful. Basically, the for...in loop will always deal with property names and an object. In the case of list(friends), I can pass in an object, friends, it will give me property names, bill and steve. But can you clarify the if statement in the search function i provided? if(friends[prop].firstName === name) I can see that the search function will only take firstName values of "Steve" or "Bill". But I don't know why that [prop] code is in the if condition statement.
If you see my explanation on how to access property values, you will see friends[prop] is equivalent to frieds['steve'] (or friends.steve) and .bill. friends['steve'].firstName is "Steve", so it's used in the comparison to the name parameter.
0

obj is a parameter passed in list function. for iterated through names of that obj. For example:

var a = {x:5}

for(var prop in a){
    //here prop will be 'x' and a[prop] will be 5 (a['x'] is 5)
    //same goes again for objects that have multiple properties
}

In your case:

var list = function(obj) {
  for(var prop in obj) {
    console.log(prop); // here will be logged Bill and Steve, because they are properties of friends -object
  }
};

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.