Prototype: Try.allThese()
Thursday, April 13, 2006Here's a little addition to the Prorotype library I needed. Prototype already has a Try.these() function in which you can pass a bunch of function and it will return the return value from the first sucessful one. It's useful in cross-browser object instantiation. But I needed a version that would try all of the functions executing as many as possible. For me it'ss useful in situations where you have to do a lot of
if(object) { /* do something with object */ } Anyway here it is, grab it if you think you could make use of it.
Try.allThese = function(){
var returnValue = [];
for (var i = 0; i < arguments.length; i++) {
var lambda = arguments[i];
try {
returnValue.push(lambda());
} catch (e) {
returnValue.push(false);
}
}
return returnValue;
};
1 Comment
Nice~