Copyright © tutorialspoint.com

Prototype Try.these Method

previous next


Advertisements

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 ie. false

Syntax:

Try.these(Function...);

Return Value:

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.


previous next

Copyright © tutorialspoint.com