0
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?

user2734550
  • 882
  • 1
  • 10
  • 29

2 Answers2

2

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.

John Kugelman
  • 330,190
  • 66
  • 504
  • 555
2

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.

lc.
  • 109,978
  • 20
  • 153
  • 183