Object keys are always strings (or converted to string) you can reference
it with dot or with square brakets and string...
foo.bar
does the same than
foo["bar"]
always, but you can do also
var thekey = "key";
foo[thekey]...
Some useful methods:
thekey in foo // will return true if foo has a property named thekey
Object.keys(foo) // returns an array of string with all enumerable
properties
El miércoles, 12 de diciembre de 2012, spqr escribió:
>
> Thanks for your help on this, I think you solved my problem with "this" by
> helping me avoid eval().
> Coming at JavaScript with some history in lisp, maybe my question should
> have been "why does JavaScript have eval()?" ;-)
> (Hah! I know it still has its uses...)
>
> I just constructed this code:
>
> var myobjs = { p1234: { a: 1,
> b: function() { var c = this.a; return 0; }},
> p2345: { a: 2,
> b: function() { var c = this.a; return 1; }}};
>
> var q = "p1234";
> myobjs[q].b();
>
> I ran a quick test and it works like a charm. I noticed that the object
> keys can be
> literals or strings and it still works. Is there any preference on which
> to use?
>
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to
> [email protected]<javascript:_e({}, 'cvml', '[email protected]');>
> To unsubscribe from this group, send email to
> [email protected] <javascript:_e({}, 'cvml',
> 'nodejs%[email protected]');>
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en