Prototype: Try.allThese()

Thursday, April 13, 2006

Here'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

#1
On the December 24, 2007, GoolER~ wrote:

Nice~

Leave your comment


HTML tags will not work. Your email address will never be displayed. If you have a Gravatar we'll display it.

Random outings from a chaotic mind

The Dexagogo Rocket Australian Web Industry Association logo

Delicious

Twitter