Standard approach:
var a = b && b.c && b.c.d && b.c.d.e;
is quite fast but not too elegant (especially with longer property names).
Using functions to traverse JavaScipt object properties is neither efficient nor elegant.
Try this instead:
try { var a = b.c.d.e; } catch(e){}
in case you are certain that a
was not previously used or
try { var a = b.c.d.e; } catch(e){ a = undefined; }
in case you may have assigned it before.
This is probably even faster that the first option.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…