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);
}
Written by Nick Jacob
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Related Tags
#javascript
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#