Hello, I finished this problem but have an issue with the instructions. How does function(propPrefix) not return s + str when called in const someProp? Does propPrefix combine with Name to form to form propName hence, leading to the statement
console.log(someObj[someProp]); calling the output of the function someObj & someProp leading to the string “John”?
Another way you can use this concept is when the property’s name is collected dynamically during the program execution, as follows:
const someObj = {
propName: "John"
};
function propPrefix(str) {
const s = "prop";
return s + str;
}
const someProp = propPrefix("Name");
console.log(someObj[someProp]);
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.
Challenge: Basic JavaScript - Accessing Object Properties with Variables
Link to the challenge: