javascript - For .. in loop? -
i'm losing here.. extremely confused how loop works.
from w3 schools:
var person={fname:"john",lname:"doe",age:25}; (x in person) { document.write(person[x] + " "); } person object properties right? how properties being accessed brackets? thought arrays?
why work, , shouldn't this?:
var person=[]; person["fname"] = "john"; person["lname"] = "doe"; person["age"] = "25"; (x in person) { document.write(person[x] + " "); }
there 2 ways in have access object's properties:
obj.keyobj['key']
the advantage of second method can provide key dynamically, e.g. obj[x] in example. obj.x literally mean x property (i.e. obj['x']), not want.
arrays work brackets, brackets not limited arrays. arrays under hood objects, designed numeric keys. can still add properties non-numeric keys them, that's not designed for.
Comments
Post a Comment