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
  • 952
  • 1
  • 12
  • 35

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
  • 349,597
  • 67
  • 533
  • 578
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.
  • 113,939
  • 20
  • 158
  • 187