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 suppose it works otherwise. Also, there's no
way to detect errors (maybe you require those vars to be set) this way.

If you want these kind of key lookup storage structures, why not create a
function to do it for you? Something like...

var Config = {bridge:{meta:{name:'peter',age:29}}};
function get(key){
    var arr = key.split('.'); // ['bridge','meta','name']
    var result = Config;
    while (arr.length && (result = result[arr.shift()]))
console.log(result);
    return result;
};
var name = get('bridge.meta.name'); // peter
var age = get('bridge.meta.age'); // 29
var occupation  = get('bridge.meta.children'); // undefined

The build up of such structure would be similar. I'll leave that up to you
:)

Similarly, to cover error checking, you could do a hasPath in a similar
fashion. This allows you to abstract that "does it exist" logic away and
leave your business logic in plain view.

Just my two cents :)

- peter

-- 
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

Reply via email to