var ni = {'hello': 23, 'he':'h', hao: 45};
for( var propertyName in ni) {
console.log(ni[propertyName]) //23,'h',45
console.log(ni.propertyName) // undefined 3 times?
}
what is the reason ni.propertyName doesn't work here?
var ni = {'hello': 23, 'he':'h', hao: 45};
for( var propertyName in ni) {
console.log(ni[propertyName]) //23,'h',45
console.log(ni.propertyName) // undefined 3 times?
}
what is the reason ni.propertyName doesn't work here?
ni.propertyName
is equivalent to ni["propertyName"]
: it gets the value of a property literally named "propertyName"
. ni[propertyName]
on the other hand uses your propertyName
variable for the lookup.
ni.propertyName
is static code that references the property named propertyName
in ni
(which does not exist). Note this is equivalent to ni["propertyName"]
.
ni[propertyName]
dynamically indexes into ni
to find the property named with the value of propertyName
.