prop
is a variable containing a string so you need to use me[prop]
like this:
for(var prop in me){
console.log(me[prop]);
}
When your property name is in a variable, you need to use the [prop]
syntax which in your case would be me[prop]
.
FYI, me.prop
tries to access a property named "prop"
. It does not use the contents of the variable prop
. You can only use the dot syntax to access a property when the property name is a constant that is known at code parse time and when the characters in the property are legal to use in the Javascript dot syntax. If it's in a variable (or if it contains characters like a .
or a space), then you must use the bracket syntax which will fetch the contents of the variable and use that property name.