Michael, If you've gone that far why not just have Person have a getZip() method to encapsulate the null checking?
On Feb 15, 7:37 am, "Michael Haufe (TNO)" <t...@thenewobjective.com> wrote: > Looks like a code smell of bad design. If you have a Person object it > would be unnecessary to do member sniffing tests and literal > injections like this. > > So instead of this: > > var person = {address: {zip: 1234}}, > person2 = {}; > console.log("Person2 undefined: " + > ((person2||{}).address||{}).zip || "no zip"); > > I'd say do this: > > var person = new Person(new Address(1234)), > person2 = new Person(); > console.log(person2.zip || "no zip"); > > On Feb 14, 5:02 pm, 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 person = {address: {zip: 1234}}, > > person2 = {}; > > console.log("Person2 undefined: " + > > ((person2||{}).address||{}).zip || "no zip"); > > > This takes the place of code like this: > > > if(person2 && person2.address) > > console.log("Person2 zip: " + person2.address.zip); > > > Does the code lose readability when done with that sort of shorthand? > > Also, is there the possibility of a performance hit for creating > > multiple empty objects? > > > [1]http://osteele.com/archives/2007/12/cheap-monads -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com