var foo = function(o) {...
Parameter o may be null, or it may or may not contain certain nested objects.
I want to find the value of o.foo.bar.baz.chomp.mumble. If any of mumble or its containing objects is null, I want to get null.
I can do this (in this case, I can assume all the path elements are either objects or don't exist -- none are non null, non objects):
var mumble = o && o.foo && o.foo.bar && o.foo.bar.baz 
   && o.foo.bar.baz.chomp ?  o.foo.bar.baz.chomp.mumble : null;
Or I can do this:
var mumble = null;
try { 
   mumble = o.foo.bar.baz.chomp.mumble;
} 
catch { //ignore null pointer exception}
Or I can do this:
var nestedVal = function( o, a ) { 
    if( isString( a ) ) {
        a = a.split( '.' );
    }
    var i = 0, j = a.length;
    for( ; o && isObject( o ) && i < j; ++i) { 
        o = o[ a[ i ] ];
    } 
    return i == j ? o : null;
};
var mumble = nestedValue(o, "foo.bar.baz.chomp.mumble");
Which is preferable, or is there a fourth, better, way to do this?
 
     
    