When you have a multi-tiered object like a json object that say has 3 tiers
i = {'id':1{'name':'austin', 'lives':'college'{'name':'eckerd', 'major':'compsci'}}}
To reference the object is it better to reference it like so
for (x in i)
i[x]['lives']['name']
//or
i[x].lives.name
I think that gets my idea a开发者_如何学Pythoncross. Pretty much use Associative arrays or the 'dot' method and why?
i[x].lives.name
is equivalent to i[x]["lives"]["name"]
.
i[x][lives][name]
means that you have variables called lives
and name
that you want to reference:
There's no real benefit to using one form over the other; imho it's clearest to use the dot notation unless you need the variable property names.
"Values can be retrieved from an object by wrapping a string expression in a [ ] suffix. If the string expression is a string literal, and if it is a legal JavaScript name and not a reserved word, then the . notation can be used instead. The . notation is preferred because it is more compact and reads better."
- Crockford, D. (2008), JavaScript: The Good Parts. (pp. 21). Sebastopol, CA, U.S.A.: O'Reilly.
精彩评论