- Prototype Tutorial
- Prototype - Home
- Prototype - Short Overview
- Prototype - Useful Features
- Prototype - Utility Methods
- Prototype - Element Object
- Prototype - Number Processing
- Prototype - Strings Processing
- Prototype - Array Processing
- Prototype - Hash processing
- Prototype - Basic Object
- Prototype - Templating
- Prototype - Enumerating
- Prototype - Event Handling
- Prototype - Form Management
- Prototype - JSON Support
- Prototype - AJAX Support
- Prototype - Expressing Ranges
- Prototype - Periodical Execution
- Prototype Useful Resources
- Prototype - Quick Guide
- Prototype - Useful Resources
- Prototype - Discussion
Prototype - Try.these Method
The Try.these() function makes it easy when you want to try different function calls, until one of them works.
It takes a number of functions as arguments and calls them one by one, in sequence, until one of them works, returning the result of that successful function call.
If none of the blocks succeeded, Try.these will return undefined, i.e., false.
Syntax
Try.these(Function...);
Return Value
First OK result.
Example
There are different ways to create XMLHttp object in different browsers. Using the Try.these() function we can return the one that works.
<html> <head> <title>Prototype examples</title> <script type = "text/javascript" src = "/javascript/prototype.js"></script> <script> createXMLHttp: function() { return Try.these( function() { return new XMLHttpRequest() }, function() { return new ActiveXObject('Msxml2.XMLHTTP') }, function() { return new ActiveXObject('Microsoft.XMLHTTP') } ) || false; } </script> </head> <body> ...... </body> </html>
If none of the blocks succeeded, Try.these will return undefined, which will cause the createXMLHttp method in the example above to return false, provided as a fallback result value.
prototype_utility_methods.htm
Advertisements