Hello All,
I have a question about the Accessing Object Properties with Variables exercise or rather more from the example given within the course material in regards to bracket notation.
Within the course material is given this example below:
var someObj = {
propName: "John"
};
function propPrefix(str) {
var s = "prop";
return s + str;
}
var someProp = propPrefix("Name");
console.log(someObj[someProp]);
I’m having trouble understanding what is the purpose of the propPrefix function? Why do we need this function in order to print out just the name John? I’ve tried console.log(someObj); which outputs to the console { propName: ‘John’ } which I see it is literally printing out the someObj object but I’m having issues understanding how the propPrefix function when added as the value for the variable someProp is needed and what exactly is happening when someProp is being added to the console.log so in other words why do we need someProp to be added like this: console.log(someObj[someProp]) to just print out the name john? I apologize if my wording is bad , I find asking questions challenging and I know this is an area I need to get better in. also I’m aware this is an example given to understand bracket notation I just cant seem to follow the logic.
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
.
Challenge: Accessing Object Properties with Variables
Link to the challenge: