Another way you can use this concept is when the property’s name is collected dynamically during the program execution, as follows:
var someObj = {
propName: "John"
};
function propPrefix(str) {
var s = "prop";
return s + str;
}
var 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.
Blockquote
I don’t understand how the entire code work. Can someone explain it?
Alright I understand how it works now. Thanks a lot Jessica! But do you mind telling me how is this concept useful? If I were to access the value of a property I could just console.log(someObj[propName]) instead of doing all the concatenation or accessing var or calling functions etc.
I guess it could be useful if you are testing it with multiple strings.
Or if you don’t know the string ahead of time and you are getting that data from somewhere like user input.