Decoding the code -

Tell me about it…

What exaclty is “prop”, “Name” and str? And how does it connect to printing the name “John”?

const someObj = {
  propName: "John"
};

function propPrefix(str) {
  const s = "prop";
  return s + str;
}

const someProp = propPrefix("Name");
console.log(someObj[someProp]);

“prop” is a string, as is “Name”. str is a function parameter that assumes the value you pass into the function. So for the line:

const someProp = propPrefix("Name");

The string “Name” is being passed into the function propPrefix and thus the function parameter str will have the value “Name” in the function body.

The code above is assigning a value to the variable someProp. That value is then being used to look up the associated property in the object someObj.

Do you understand what the value of someProp is after the function returns a value?

1 Like

How does the code above “Name” relates / links to “Name”?

I see a propName: “John”, but I see nothing that says “Name”.

What is the value of someProp? In other words, what does the function propPrefix return when you pass the string “Name” into it?

Those are good questions that I don’t know. This is coming from this part of the course in this link here:

It says:

someProp would have a value of the string propName , and the string John would be displayed in the console.

Note that we do not use quotes around the variable name when using it to access the property because we are using the value of the variable, not the name .

const someObj = {
  propName: "John"
};

function propPrefix(str) {
  const s = "prop";
  return s + str;
}

const someProp = propPrefix("Name");
console.log(someObj[someProp]);

I think you might want to revisit this challenge:

Return a Value from a Function with Return

They are trying to access a property from the object, but I see no link between the rest of the code and the someObj above it. propName and “John” is not showing in the rest of the code below of it.

Once you know what value the variable someProp holds then I think it will all fall into place.

someProp is supposed to have the value of “John” coming from the object, going through the propPrefix function.

I don’t see how the propPrefix funtion is tapping into the someObj.

I did the test on this section and it was simple, but this last example I am not connecting the dots… I am thinking they are replacing the actual working elements with the examples of str, “prop”, and “Name” to make it clearer, but I am just not getting it.

someProp has the value returned by the function propPrefix with the string “Name” passed into it. What is that value?

I think “Name” doesn’t have a real value.

I think they are using “Name” more like an example.

The entire code is what you saw there.

They are trying to explain how to access a value of an object.

A very short read on the entire example is here:

I understand how this works, no need to explain to me :slight_smile:

You keep ignoring my question. What is the value of someProp? You won’t understand how this all fits together until you know that. I gave you a link to a previous challenge that explains how functions work. Do you understand how functions work? If so then you should hopefully be able to tell me what the value of someProp is.

I don’t think we can go any further until you get the value of someProp.

someProp would have a value of the string propName , and the string John would be displayed in the console.

Still don’t see how they are connected.

I totally understand all of the other examples, but this last sample is written in a different way.

To be more specific, I don’t see the connection between proPrefix and someObj…

Given that the test, and the previous sample right above this sample are way easier… I am just going to qualify this sample as a bad sample.

I am speculating that the keywords of str, “prop”, and “Name” are not actually connected to a functional code, but that they are using it as sample of what would go there, rather than including an actual value of what would go there to make the code work.

No, it is not.

This makes no sense. Look at the code in the function propPrefix. What does it return if you pass in the value “Name”. That’s what the value of someProp will be and then you will see how this all fits together.

1 Like

From:
Accessing Object Properties with Variables

someProp would have a value of the string propName ,

^^
That is the explanation written for this code in this part of the course.

Ahh, I see now, I misread what you said earlier. Usually, when you say a variable has the value of a string you put that string in quotes:

someProp = "propName"

Is this what you meant? And if so, then can you see how

someObj[someProp]

gives you the value “John”?

Yes, its getting clearer, but I am not trying too hard.

It is a non working sample with the words changed to make a sample, but the sample doesn’t make much sense because they are not using the proper words to make it work.

Why don’t they just use “propName” instead of “someProp”? Or whatever else they are not using?

Anyway… I think I am good for now… thanks…

They are not using the string “someProp”. They are using the variable someProp to store the string “propName”.

someObj[someProp] == someObj["propName"]

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