[JSMentors] Nested Property Access

2011-02-15 Thread Sam Merrell
Hi all , What are the groups thoughts on safely accessing a nested objects value that may be undefined? I recently came across a blog post [1] from Oliver Steele where he did a shortcut that looks like this:     var person = {address: {zip: 1234}},        person2 = {}; console.log(Person2

Re: [JSMentors] Nested Property Access

2011-02-15 Thread Andrew Dodson
Ha awesome. Took me a minute to get it and now I quite like it. However some kind of intellisense is usually at hand to save repeatedly writing out the parent objects. On 15 Feb 2011 09:16, Sam Merrell merrell@gmail.com wrote: Hi all , What are the groups thoughts on safely accessing a

Re: [JSMentors] Nested Property Access

2011-02-15 Thread Adrian Olaru
((person2||{}).address||{}).zip || no zip); Actually this takes the place of code like this: (person2 person2.address person2.address.zip) ? person2.address.zip : no zip; You can also use an if statement if you want. Does the code lose readability when done with that sort of shorthand?

Re: [JSMentors] Nested Property Access

2011-02-15 Thread Peter van der Zee
On Tue, Feb 15, 2011 at 12:08 PM, Adrian Olaru agol...@gmail.com wrote: ((person2||{}).address||{}).zip || no zip); I find it harder to determine what's actually going on. But maybe it just takes a little time getting used to. Clearly not the way to go for apps where speed is important, but I

Re: [JSMentors] Nested Property Access

2011-02-15 Thread Diego Perini
On Tue, Feb 15, 2011 at 12:02 AM, Sam Merrell merrell@gmail.com wrote: Hi all , What are the groups thoughts on safely accessing a nested objects value that may be undefined? I recently came across a blog post [1] from Oliver Steele where he did a shortcut that looks like this:     var 

Re: [JSMentors] Nested Property Access

2011-02-15 Thread Peter van der Zee
On Tue, Feb 15, 2011 at 1:59 PM, Diego Perini diego.per...@gmail.comwrote: I believe this is almost equivalent: try { a = person2.address.zip; } catch(err) { a = 'no zip'; } Oh, actually I like that. Maybe even better unless you do it a lot. - peter -- To view archived discussions from

Re: [JSMentors] Nested Property Access

2011-02-15 Thread Peter van der Zee
On Tue, Feb 15, 2011 at 3:04 PM, Dmitry Pashkevich dip...@mail.ru wrote: I am not very savvy in using exceptions... Are there any pitfalls with their use in such case? There's a small performance hit (which may vary across engines). And possibly some odd collisions with the exception variable

Re: [JSMentors] Nested Property Access

2011-02-15 Thread Asen Bozhilov
Sam Merrell: What are the groups thoughts on safely accessing a nested objects value that may be undefined? Firstly you should answer on the question, are you going to create objects which properties value may be undefined? I recently came across a blog post [1] from Oliver Steele where he