Tutorials Point

  Learn Prototype
  Prototype Resources
  Selected Reading

Copyright © 2014 by tutorialspoint

  Home     References     Discussion Forums     About TP  

Prototype Try.these Method

previous next AddThis Social Bookmark Button


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



Return Value:

  • First OK result.


There are different ways to create XMLHttp object in different browsers. Using the Try.these() function we can return the one that works.

<title>Prototype examples</title>
<script type="text/javascript" 

createXMLHttp: function()
   return Try.these(
    function() { return new XMLHttpRequest() },
    function() { return new ActiveXObject('Msxml2.XMLHTTP') },
    function() { return new ActiveXObject('Microsoft.XMLHTTP') }
  ) || false;



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 Printer Friendly