Last Updated: February 25, 2016
·
496
· nickjacob

Safely call nested methods

Want to call a nested method on an object without having to worry about causing an error (like coffeescript's existential operator, ?)

// mthd: full path to method, args - array of arguments
myObj.prototype.callnested = function(mthd,args){
    var m = mthd.split('.'),o=this;
    for(var i=0,l=m.length; i < l; i++){
        if(o[m[i] === void(obj = o[m[i]])) return void(0);
    }
    return o.apply(this,args);
}